require('swiper/swiper-3.4.2.jquery.min.js');
var cookie = require('cookie')

class da {
    constructor() {
        this.dashow = false//广告的显示状态
        this.init()
    }
    init() {
        var $dawrap = $('.da-container')
        if (!cookie.get('dw_hide_index_da')) {
            $dawrap.show()
            this.dashow = true
            
            var $das = $dawrap.find('a')
            if ($das.length > 1) {
                $das.addClass('swiper-slide')

                new Swiper('.da-container', {
                    autoplay: 4000,
                    autoplayDisableOnInteraction: false,
                    loop: true,
                    pagination: '.swiper-pagination',
                })
            }

            this.daClose()
            this.fixedDa()
        }
    }
    daClose() {
        var self = this
        $('body').on('click', '.da-close', function () {
            $(this).closest('.da-container').remove()
            $('.da-like-block').remove()
            self.dashow = false
            var date = new Date()
            date.setTime(date.getTime() + 1000 * 60 * 60 * 3)
            cookie.set('dw_hide_index_da', true, {
                expires: date
            })
        })
    }

    fixedDa() {
        if (this.dashow) {
            var self = this
            var $win = $(window)
            var $container = $('.da-container')
            var $dalike = $('.da-like-block')
            var daTop = $win.height() / 2
            var isIOS = this.isIOS()

            if (!isIOS) {
                //如果不是ios机型
                $dalike.css('height', `${$container.height()}px`)
                $win.on('scroll', function () {
                    if ($win.scrollTop() >= daTop) {
                        $container.addClass('fixed')
                        $dalike.show()
                    } else {
                        $container.removeClass('fixed')
                        $dalike.hide()
                    }
                })
            } else {
                $win.on('scroll touchmove touchend', function () {
                    if ($win.scrollTop() >= daTop) {
                        $container.addClass('sticky')
                    } else {
                        $container.removeClass('sticky')
                    }
                })
            }

        }
    }
    isIOS() {
        return !!window.navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    }
}

$(window).load(() => {
    new da
})
