<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 class="button-label">
                    <div class="label-wrapper">
                        <p class="m-0">
                            <span
                                class="mt-1"
                                v-html="buttonLabel"></span>
                        </p>
                        <p
                            v-if="buttonLabelSubCopy"
                            class="sub-label m-0"
                            v-html="buttonLabelSubCopy">
                        </p>
                    </div>
                    <div class="pricing-info-mobile">
                        <span
                            class="price"
                            v-html="buttonAuxiliaryCopy"></span>
                    </div>
                </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 mr-3"
                        v-html="buttonFooterCopy">
                    </div>
                </div>
            </div>
            <div class="pricing-info-desktop">
                <span
                    class="price"
                    v-html="buttonAuxiliaryCopy"></span>
            </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>
// TO DO: look into why the font renders differently than Heritage, even with the same specs
    .ml-n4 {
        margin-left: -$spacer * 2;
    }
    .mr-3 {
        margin-right:0rem;
    }
    .package-main {
        .package-wrapper {
            margin: 0.5rem 0;

            .button-label {
                display: flex;
                &:before {
                    content: "";
                    position: relative;
                    top: 5px;
                    margin-right: 1rem;
                    border-radius: 50%;
                    border: 0.666667px solid #767676;
                    width: 16px;
                    height: 16px;
                    min-width: 16px;
                }
            }

            label {
                display: block;
                height: 100%;
            }

            .package-label {
                height: 100%;
                display: flex;
                flex-direction: column;
            }

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

                + .package-label {
                    display: flex;
                    flex-direction: column;
                    align-items: flex-start;
                    position: relative;
                    cursor: pointer;
                    width: 100%;
                    padding: 10px;
                    border: 0.666667px solid #dddddd;
                    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
                    border-radius: 0.25rem;
                    overflow: hidden;
                    min-height: 60px;
                    max-height: 100px;
                    @include media-breakpoint-up(md) {
                        max-height: 500px;
                    }

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

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

                &:hover {
                    + .button-label {
                        &:before {
                            border: 0.666667px solid #767676;
                            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: $background-color-selected;
                        border: 0.666667px solid $heritage-blue-secondary;
                        max-height: 500px;
                    }

                    + .button-label {
                        &:before {
                            box-shadow: 0px 0px 0px 1px $heritage-blue-primary;
                            border: none;
                        }
                    }

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

            }

            .package-footer {
                color: #af2117;
                font-weight: $font-weight-bold;
                margin-top: 20px;
                font-size: 0.875rem;
                line-height: 24px;
            }

            .package-specs {
                flex: 0;
                width: 100%;
                max-height: 0;
                transition: all 1s ease;
                @include media-breakpoint-up(md) {
                    max-height: 500px;
                }
                p {
                    color: #4d4e53;
                    font-weight: 600;
                    justify-content: space-between;

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

                ul {
                    margin: 0.5rem 0 0 1.25rem;
                    padding: 0;
                    color: $darker-gray;

                    li {
                        margin-bottom: 0.25rem;
                        font-size: 0.875rem;
                        line-height: 1.5rem;
                        color: $darker-gray;

                        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: block;
                    }
                }
            }

            .pricing-info-desktop {
                display: flex;
                flex: 1;
                align-items: flex-end;
                @include media-breakpoint-down(md) {
                    display: none;
                }
                span {
                    &.price {
                        color: #075f35;
                        font-size: 0.875rem;
                        font-weight: $font-weight-bold;
                        padding: 0.35rem 0 1rem 0;
                    }
                }
            }

            .pricing-info-mobile {
                display: flex;
                flex: 1;
                justify-content: flex-end;
                margin-top: 0.25rem;
                @include media-breakpoint-up(md) {
                    display: none;
                }
                span {
                    &.price {
                        color: #075f35;
                        font-size: 0.875rem;
                        font-weight: $font-weight-bold;
                    }
                }
            }
        }
    }
</style>
