<template>
    <baseInputButton
        v-bind="$props"
        v-model="selectedValue">
        <div
            class="package-label mb-4"
            :class="[buttonLabelSubCopy ? 'has-subheader' : '']"
            for="testradio">
            <div class="package-specs">
                <div>
                    <p class="m-0">
                        <span v-html="buttonLabel"></span>
                        <span
                            class="pricing-info"
                            v-html="buttonAuxiliaryCopy"></span>
                    </p>
                    <p
                        v-if="buttonLabelSubCopy"
                        class="sub-label m-0"
                        v-html="buttonLabelSubCopy">
                    </p>
                </div>
                <div class="hide-when-closed">
                    <ul>
                        <li
                            v-for="listItem in arrayOfListItemsFromBodyText"
                            :key="listItem"
                            class="mb-0">
                            <!-- no textLink -->
                            <span
                                v-if="!doesCopyContainTextLink(listItem)"
                                v-html="listItem"></span>
                            <!-- with textLink -->
                            <template
                                v-for="copy in splitCopyOnCMSPlaceHolder(listItem)"
                                v-else
                                :key="copy">
                                <span
                                    v-if="!doesCopyContainTextLink(copy)"
                                    v-html="copy"></span>
                                <span v-else>
                                    <textLink
                                        linkType="text"
                                        :text="getRouterLinkDisplayTextFromCopy(copy)"
                                        href="#!"
                                        :data-bs-target="'#' + getRouterLinkRouteFromCopy(copy)"
                                        @clickEvent="textLinkEmit(copy)" />
                                </span>
                            </template>
                        </li>
                    </ul>
                    <!-- End of body text parsing -->
                    <!--ms-n6-->
                    <div
                        v-if="buttonFooterCopy"
                        class="package-footer fw-bold caption mt-4 ml-n4 mr-3"
                        v-html="buttonFooterCopy"></div>
                </div>
            </div>
        </div>
    </baseInputButton>
</template>

<script>
import baseInputButton from '@/digital-components/base-input-button/base-input-button.vue';
import textLink from '@/ux-components/text-link/text-link.vue';
import inputButtonWrapperMixin from '@/mixins/input-button-wrapper-mixin';
import {
    getRouterLinkDisplayTextFromCopy,
    getRouterLinkRouteFromCopy,
    splitCopyOnCMSPlaceHolder,
    doesCopyContainTextLink
} from '@/helpers/cms-content-helper';

export default {
    name: 'service-package-radio',
    components: {
        baseInputButton,
        textLink
    },
    mixins: [inputButtonWrapperMixin],
    emits: ['link-event'],
    computed: {
        arrayOfListItemsFromBodyText() {
            return this.getArrayOfListItemsFromRawCmsCopy(this.buttonBodyCopy);
        }
    },
    methods: {
        getRouterLinkDisplayTextFromCopy,
        getRouterLinkRouteFromCopy,
        splitCopyOnCMSPlaceHolder,
        doesCopyContainTextLink,
        stripUlTagFromCopy(copy) {
            return copy;
        },
        getArrayOfListItemsFromRawCmsCopy(copy) {
            if (copy) {
                return copy
                    .split('- ') // At some point we'll want a better delimiter
                    .filter((lineItem) => lineItem);
            }
            return null;
        },
        textLinkEmit(copy) {
            this.$parent.$emit('link-event', {
                args: getRouterLinkRouteFromCopy(copy)
            });
        }
    }
};
</script>

<style lang="scss" scoped>
    .ml-n4 {
        margin-left: -$spacer * 2;
    }
    .mr-3 {
        margin-right:0rem;
    }
    .package-main {
        .package-wrapper {
            margin: 0.5rem 0;

            label {
                display: block;
            }

            input[type="radio"] {
                opacity: 0;
                position: absolute;
                left: -9999px;

                + .package-label {
                    display: flex;
                    align-items: flex-start;
                    position: relative;
                    cursor: pointer;
                    width: 100%;
                    padding: 1rem;
                    border: 1px solid $gray-300;
                    box-shadow: 0px 4px 8px -4px rgba(0, 0, 0, 0.15), 0px 4px 24px -8px rgba(0, 0, 0, 0.2);
                    border-radius: 0.5rem;
                    overflow: hidden;
                    min-height: 60px;
                    max-height: 100px;
                    @include media-breakpoint-up(md) {
                        max-height: 500px;
                    }

                    &.has-subheader {
                        min-height: 86px;
                    }

                    &:before {
                        content: "";
                        position: relative;
                        top: 5px;
                        margin-right: 1rem;
                        border-radius: 50%;
                        border: 1px solid $gray-500;
                        width: 16px;
                        height: 16px;
                        min-width: 16px;
                    }

                    &:after {
                        content: "";
                        position: absolute;
                        left: 19px;
                        top: 24px;
                        border-radius: 50%;
                        width: 10px;
                        height: 10px;
                        min-width: 10px;
                    }
                }

                &:hover {
                    + .package-label {
                        &:before {
                            border: 1px solid #8e9292;
                            box-shadow: 0px 0px 0px 4px #9fcee6, 0px 1px 4px rgba(0, 0, 0, 0.2);
                        }
                    }
                }

                &:checked {
                    + .package-label {
                        .package-specs {
                            max-height: 1000px;
                        }
                    }

                    + .package-label {
                        .package-specs {
                            .hide-when-closed {
                                display: block;
                            }
                        }
                    }

                    + .package-label {
                        background-color: $blue-100;
                        border: 1px solid $blue;
                        max-height: 500px;
                    }

                    + .package-label {
                        &:before {
                            box-shadow: 0px 0px 0px 1px $blue;
                        }
                    }

                    + .package-label {
                        &:after {
                            background: $blue;
                        }
                    }
                }

                &:focus {
                    + .package-label {
                        &:before {
                            border: 2px solid $blue;
                        }
                    }
                }
            }

            .package-footer {
                color: $red;
                font-weight: $font-weight-bold; // 600 in fmg
                margin-top: 0.5rem; // not in fmg
            }

            .package-specs {
                display: flex;
                flex-direction: column;
                width: 100%;
                max-height: 0;
                transition: all 1s ease;
                @include media-breakpoint-up(md) {
                    max-height: 500px;
                }
                p {
                    font-weight: $font-weight-bold; // 600 in fmg
                    display: flex;
                    justify-content: space-between;

                    span {
                        &.pricing-info {
                            color: $green;
                            font-size: 0.875rem;
                        }
                    }

                    &.sub-label {
                        color: $green;
                        text-transform: uppercase;
                        font-size: 0.75rem;
                    }
                }

                ul {
                    margin: 1rem 0 0 -.6rem; // .9375rem in fmg
                    padding: 0;

                    li {
                        margin-bottom: 0.5rem;
                        font-size: 0.875rem;
                        line-height: 1.5rem;

                        a {
                            line-height: 1.5rem;
                            padding: 0;
                        }
                    }
                }

                @keyframes slideaway {
                    from {
                        display: block;
                    }

                    to {
                        transform: translateY(40px);
                        opacity: 0;
                    }
                }

                .hide-when-closed {
                    display: none;
                    @include media-breakpoint-up(md) {
                        display: flex;
                        flex-direction: column;
                    }
                }
            }
        }
    }
</style>
