var Wick = Wick || {};

Wick.CheckoutConfirmationNew = function () {

  const checkConfVar = {
    $storeInfoBlock: $('.order-block__store-info'),
    $orderBlockAddress: $('.order-block__address'),
    $showInfoBtn: $('.show-info'),
    $hideInfoBtn: $('.hide-info'),
    $productLineBlock: $('.order-block__product-line'),
    $productInfoBlock: $('.order-block__product-info'),
    $showProductsBtn: $('.show-products'),
    $hideProductsBtn: $('.hide-products'),
    $sDetailsCopy: $('.s-details__copy'),
    $sDetailsAddress: $('.contact-block__address .address-text'),
    $contactBlockAddress: $('.contact-block__address'),
    orderBlock: ".order-block"
  };

  function init() {
    checkConfVar.$showInfoBtn.on('click', function () {
        $(this).closest(checkConfVar.$orderBlockAddress).find(checkConfVar.$storeInfoBlock).slideDown();
        $(this).closest(checkConfVar.$orderBlockAddress).find(checkConfVar.$showInfoBtn).addClass('d-none');
        $(this).closest(checkConfVar.$orderBlockAddress).find(checkConfVar.$showInfoBtn).removeClass('d-block');
    });

    checkConfVar.$hideInfoBtn.on('click', function () {
        $(this).closest(checkConfVar.$orderBlockAddress).find(checkConfVar.$storeInfoBlock).slideUp();

        setTimeout(function( that ){
            $(that).closest(checkConfVar.$orderBlockAddress).find(checkConfVar.$showInfoBtn).addClass('d-block');
            $(that).closest(checkConfVar.$orderBlockAddress).find(checkConfVar.$showInfoBtn).removeClass('d-none');
        }, 400, this);

    });

    checkConfVar.$showProductsBtn.on('click', function () {
      $(this).closest(checkConfVar.$productLineBlock).find(checkConfVar.$productInfoBlock).slideDown();
      $(this).closest(checkConfVar.$productLineBlock).find(checkConfVar.$showProductsBtn).addClass('d-none');
      $(this).closest(checkConfVar.$productLineBlock).find(checkConfVar.$showProductsBtn).removeClass('d-block');
      $(this).closest(checkConfVar.$productLineBlock).find(checkConfVar.$hideProductsBtn).addClass('d-block');
      $(this).closest(checkConfVar.$productLineBlock).find(checkConfVar.$hideProductsBtn).removeClass('d-none');
    });

    checkConfVar.$hideProductsBtn.on('click', function () {
      $(this).closest(checkConfVar.$productLineBlock).find(checkConfVar.$productInfoBlock).slideUp();

      setTimeout(function( that ){
          $(that).closest(checkConfVar.$productLineBlock).find(checkConfVar.$showProductsBtn).addClass('d-block');
          $(that).closest(checkConfVar.$productLineBlock).find(checkConfVar.$showProductsBtn).removeClass('d-none');
          $(that).closest(checkConfVar.$productLineBlock).find(checkConfVar.$hideProductsBtn).addClass('d-none');
          $(that).closest(checkConfVar.$productLineBlock).find(checkConfVar.$hideProductsBtn).removeClass('d-block');
      }, 400, this);

        });

        checkConfVar.$sDetailsCopy.on('click', function (e) {
            console.log("cop")
            e.preventDefault();
            var $temp = $('<textarea>').css({
                position: 'absolute',
                top: 0,
                left: 0,
                opacity: 0
            });
            $temp.prop('disabled', true);
            $(this.parentElement).append($temp);//workaround for iPad scrolling
            var text = $(this).closest(checkConfVar.orderBlock).find(checkConfVar.$sDetailsAddress).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[0].setSelectionRange(0, 999999);
            } else {
                $temp.select();
            }

            document.execCommand("copy");
            $(this).addClass(checkConfVar.copyLinkHovered);
            $temp.remove();
        });

      function iosCopyToClipboard($input) {
          var el = $input.get(0);
          var editable = el.contentEditable;
          var readOnly = el.readOnly;

          el.contentEditable = true;
          el.readOnly = false;
          var range = document.createRange();
          range.selectNodeContents(el);
          var sel = window.getSelection();
          sel.removeAllRanges();
          sel.addRange(range);
          el.setSelectionRange(0, 999999);
          el.contentEditable = editable;
          el.readOnly = readOnly;
      }
    }

  init();
}();
