var Wick = Wick || {};
Wick.OrderItemCC = (function () {
    const cc = {
        $content: $('.content'),
        $component: $('.cc-order'),
        $changeLink: $('.cc-order__change'),
        $storeUnselected: $('.cc-order__store-unselected'),
        $closeLink: $('.cc-order__close-link'),
        $info: $('.cc-order__info'),
        $storeMore: $('.cc-order .store__more_link'),
        $storeInfo: $('.store-info'),
        $storeInfoName: $('.store-info__name-name'),
        $storeInfoHide: $('.store-info__hide-info'),
        $storeName: $('.store__name-text'),
        $store: $('.store'),
        $storeCopy: $('.store-info__copy'),
        $storeAdress: $('.store-info__address-value'),
        $storeAdressBlock: $('.store-info__address'),
        $toggleMap: $('.store-info__tgl-map'),
        $toggleMapText: $('.store-info__tgl-map_text'),
        $map: $('.store-info__map'),
        component: '.cc-order',
        changeLink: '.cc-order__change',
        info: '.cc-order__info',
        closeLink: '.cc-order__close-link',
        infoHide: 'cc-order__info-hide',
        mapHide: 'store-info__map_hide',
        changeHide: 'cc-order__change-hide',
        storeUnselected: '.cc-order__store-unselected',
        storeInfoHide: 'store-info-hide',
    }

    function init() {

        function toggleInfo(context) {
            $(context).closest(cc.component).find(cc.changeLink).toggleClass(cc.changeHide);
            $(context).closest(cc.component).find(cc.info).toggleClass(cc.infoHide)
        }

        cc.$content.on('click', cc.changeLink, function (e) {
            e.preventDefault()
            toggleInfo(this)
        })

        cc.$content.on('click', cc.storeUnselected, function (e) {
            e.preventDefault()
            toggleInfo(this)
        })

        cc.$content.on('click', cc.closeLink, function (e) {
            e.preventDefault()
            toggleInfo(this)
        })

        cc.$storeMore.on('click', function () {
            var storeName = $(this)
                .closest('.store')
                .find(cc.$storeName)
                .text()
                .trim()
                .toLowerCase();

            if ($(this).text().trim() === 'Store Info') {
                $(this).closest(cc.$component).find(cc.$storeInfo).each(function () {
                    let storeInfoName = $(this)
                        .find(cc.$storeInfoName)
                        .text()
                        .trim()
                        .toLowerCase();
                    if (storeInfoName === storeName) {
                        $(this).removeClass(cc.storeInfoHide);
                    }
                });
            } else {
                $(this).closest(cc.$component).find(cc.$storeInfo).each(function () {
                    let storeInfoName = $(this)
                        .find(cc.$storeInfoName)
                        .text()
                        .trim()
                        .toLowerCase();
                    if (storeInfoName === storeName) {
                        $(this).addClass(cc.storeInfoHide);
                    }
                });
            }
        })

        cc.$storeInfoHide.on('click', function (e) {
            e.preventDefault();

            let storeName = $(this)
                .closest(cc.$storeInfo)
                .find(cc.$storeInfoName)
                .text()
                .trim()
                .toLowerCase();

            $(this).closest(cc.$component).find(cc.$store).each(function () {
                let storeMore = $(this).find(cc.$storeName).text().trim().toLowerCase();

                if (storeMore === storeName) {
                    $(this).find(cc.$storeMore).click()
                }
            })
        })

        cc.$storeCopy.on('click', function (e) {
            e.preventDefault();
            var $temp = $('<input>');
            $(this.parentElement).append($temp); //workaround for iPad scrolling
            var text = $(this).closest(cc.$storeAdressBlock).find(cc.$storeAdress).text().trim();
            var str = text.replace(/(^\w|\s\w)/g, function (m) {
                return m.toUpperCase()
            });
            var textFiltered = str.split(' ').filter(function (value) {
                return value !== ''
            });
            var textTrimmed = textFiltered.join(' ');
            $temp.val(textTrimmed);
            if (navigator.userAgent.match(/TradeProMobile_iOS|DIYMobile_iOS/i)) {
                iosCopyToClipboard($temp);
            } else if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
                $temp.select();
                $temp.setSelectionRange(0, 999999);
            } else {
                $temp.select();
            }

            document.execCommand("copy");
            $temp.remove();
        });

        cc.$toggleMap.on('click', function (e) {
            e.preventDefault();

            if ($(this)
                .closest(cc.$storeInfo)
                .find(cc.$toggleMapText)
                .text()
                .trim() === 'Hide Map'
            ) {
                $(this)
                    .closest(cc.$storeInfo)
                    .find(cc.$toggleMapText)
                    .text('Show Map');
                $(this)
                    .closest(cc.$storeInfo)
                    .find(cc.$map)
                    .addClass(cc.mapHide);
            } else if ($(this)
                .closest(cc.$storeInfo)
                .find(cc.$toggleMapText)
                .text()
                .trim() === 'Show Map'
            ) {
                $(this)
                    .closest(cc.$storeInfo)
                    .find(cc.$toggleMapText)
                    .text('Hide Map');
                $(this)
                    .closest(cc.$storeInfo)
                    .find(cc.$map)
                    .removeClass(cc.mapHide);
            }
        });
    }

    if (!cc.$component.length) {
        return;
    }

    init();
})();
