const baseUrl = document.getElementById('require').getAttribute('href').split('/js/main').shift();

require.config({
  baseUrl: baseUrl.includes('/tpl/') ? baseUrl : '',
  waitSeconds: 60,
  paths: {
    'jquery': 'js/lib/jquery.min',
    'gsap': 'js/gsap/gsap.min',
    'scrolltrigger': 'js/gsap/ScrollTrigger.min',
    'until': 'js/gsap/until',
    'smoothscroll': 'js/gsap/custom_slide',
    'comcus': 'js/comcus',
    'slick': 'js/slick.min',
    'domReady': 'js/lib/require-domready.min',
    'vivus': 'js/vivus',
    'swiper': 'js/swiper'
  },
  map: {
    '*': {
      'min': 'js/lib/css.min'
    }
  },
  shim: {
    'comcus': ['jquery'],
    'scrolltrigger': ['gsap'],
    'until': ['gsap']
  },
  urlArgs: 'ver=0.0.1'
});

require(['jquery', 'slick', 'swiper', 'vivus', 'smoothscroll', 'comcus', 'scrolltrigger', 'until', 'min!css/slick', 'domReady!'], (...args) => {
  console.log(''.concat('%c------  Module Loading Completed  ------'), 'color: #ffffff; font-size: 14px; background: #1677ff; padding: 8px 12px; margin: 10px 0;');

  window.Vivus = args[3];

  const rotatedDeg = 24;
  let previousRealIndex = 0;
  let currentGearAngle = 0;
  let lastIdx = 1;
 
  $('.mbanner .news .news_video').each(function () {
    if ($(this).find('video').attr('src') == '') {
      $(this).hide();
      $(this).siblings('.news_imgs').show();
    } else {
      $(this).show();
      $(this).siblings('.news_imgs').hide();
    }
  });

  $('.mbanner .news_list').slick({
    infinite: true,
    slidesToShow: 1,
    autoplay: true,
    slidesToScroll: 1,
    dots: true,
    arrows: false
  });
  const fadeSwiper = new Swiper('.swiper-fade_effect', {
    effect: 'fade',
    fadeEffect: { crossFade: true },
    loop: true,
    allowTouchMove: false,
    autoplay: false
  });

  const slideSwiper = new Swiper('.swiper-slide_effect', {
    initialSlide: 1,
    slidesPerView: 2,
    spaceBetween: 16,
    loop: true,
    allowTouchMove: false,
    autoplay: {
      delay: 5000,
      disableOnInteraction: false,
      pauseOnMouseEnter: true
    },
    navigation: {
      prevEl: '.thumb-button-prev',
      nextEl: '.thumb-button-next'
    },
    breakpoints: {
      999: {
        slidesPerView: 5,
        spaceBetween: 28
      },
      480: {
        slidesPerView: 3,
        spaceBetween: 16
      }
    }
  });

  const rotateSwiper = new Swiper('.swiper-rotate_effect', {
    loop: true,
    effect: 'fade',
    fadeEffect: { crossFade: true },
    autoplay: {
      delay: 5000,
      disableOnInteraction: false,
      pauseOnMouseEnter: true
    },
    on: {
      init() {
        previousRealIndex = this.realIndex;
      },
      slideChange(ins) {
        const current = ins.realIndex;
        const diff = current - previousRealIndex;
        const deltaAngle = -diff * rotatedDeg;

        currentGearAngle += deltaAngle;

        $('.research_gear_round').css('transform', `rotate(${currentGearAngle}deg)`);

        previousRealIndex = current;
      }
    },
    pagination: {
      el: '.custom-pagination',
      clickable: true
    },
    navigation: {
      prevEl: '.thumb-button-prev',
      nextEl: '.thumb-button-next'
    }
  });

  slideSwiper.on('slideChangeTransitionStart', () => {
    const current = slideSwiper.realIndex;
    let diff = current - lastIdx;
    if (diff > 1) diff = -1;
    if (diff < -1) diff = 1;

    if (diff === 1) fadeSwiper.slideNext();
    else if (diff === -1) fadeSwiper.slidePrev();

    lastIdx = current;
  });

  new Vivus('building', { type: 'sync', duration: 100, start: 'inViewport' }, function () {
    $('#build-modal').css('opacity', 1);
  });

  const singleSwiper = new Swiper('.single-swiper', {
    loop: true,
    effect: 'fade',
    fadeEffect: { crossFade: true },
    autoplay: {
      delay: 5000,
      disableOnInteraction: false,
      pauseOnMouseEnter: true
    },
    navigation: {
      prevEl: '.thumb-button-prev',
      nextEl: '.thumb-button-next'
    }
  });

  new GsapAnimate.init({
    scrollTopAnime: ".Scroll-to-top", //置顶图标动画
    slideNavAnime: ".Quick-navigation", //锚点导航动画
    fixHeader: $(window).width()>999, //头部固定
    isFixbanner: false,//大图全屏
    navMaxSreen: {
      control: ".menu-btn", //控制元素
      box: ".navbox", //元素
      hideDom: [".head-right .left", ".head-right .nav", ".header .searchbox"], //隐藏元素
    }, //导航咱开(不写是null)
    callback: function (target) {
      //回调函数 target:当前动画元素的元素
    }
  });

  const loaderContainer = document.querySelector('.page-loading-container');
  const loaderEle = document.querySelector('.loading-shade');
  const loaderBounce = document.querySelector('.loading-bounce');

  const loaderTimeLine = gsap.timeline({
    onComplete: () => {
      loaderContainer.remove();
      $('body').removeClass('page-overflow');
    }
  });

  loaderTimeLine.to(loaderEle, { scale: 1.5, opacity: 0, duration: 1 }).to(loaderBounce, { opacity: 0, duration: 1 }, 0);

});