var Util = require("util")

class pageGameDetail {
    constructor() {
        this.game_id = DetailUtil.getGameId()
        // this.game_id = 2030
        this.$upvote = $('.up-vote')
        this.init()
    }

    init() {
        this.changetab('.news-nav', '.news-item');
        this.changetab('.video-nav', '.video-list');
        $('.dw-nav-list').each(function (index, navs) {
            $(navs).children().eq(0).click()
        })
        $(".itd-detail").click(function () {
            $p = $(this).prev();
            if ($p.hasClass('close')) {
                $(this).find('em').removeClass('up');
                $p.removeClass('close');
            } else {
                $(this).find('em').addClass('up');
                $p.addClass('close');
            }
        })

        this.checkVote().upvote()
    }

    changetab(parent, showbox) {
        $(parent).on("click", "li", function () {
            var $this = $(this);
            var index = $this.index();

            $(this).addClass("active").siblings().removeClass("active");
            $this.closest(parent).siblings(showbox).eq(index).show().siblings(showbox).hide();

            return false;
        })
    }

    checkVote() {
        var self = this
        if (Util.isLoggedIn()) {
            $.ajax({
                url: 'http://ku.newgame.duowan.com/index.php?r=api/IsAllowRecom&type=jsonp',
                data: { game_id: this.game_id },
                dataType: 'jsonp',
                success: function (ret) {
                    if (ret && ret.status) {
                        self.$upvote.show()
                    } else {
                        self.$upvote.addClass('up-voted').text('您已点赞').show()
                    }
                },
                error: function (err) {
                    console.log(err)
                }
            })
        } else {
            self.$upvote.show()
        }


        return this
    }

    upvote() {
        var self = this

        this.$upvote.on('click', function () {
            if ($(this).hasClass('up-voted')) return;

            else if (!Util.isLoggedIn()) {
                location.href = `http://www.duowan.com/mlogin/?calbackUrl=${encodeURIComponent(location.href)}`
            } else {
                $.ajax({
                    url: 'http://ku.newgame.duowan.com/index.php?r=index/Recommend&type=jsonp',
                    data: { game_id: self.game_id },
                    dataType: 'jsonp',
                    success: function (ret) {
                        if (ret.code == 1) {
                            self.$upvote.addClass('up-voted').text('您已点赞')
                        }
                    },
                    error: function (ret) {
                        console.log(ret)
                    }
                })
            }
        })


        return this
    }
}

class DetailUtil {
    constructor() {

    }
    static getGameId() {
        var href = location.href;
        var gameReg = /([0-9]+).html$/;

        return gameReg.exec(href)[1]

    }
}

class LoadMore {
    constructor(selector) {
        this.$loadMoreBtn = $(selector)
        this.init().checkList().loadmore()
    }

    init() {
        this.$loadMoreBtn.each(function (index) {
            var $this = $(this),
                pageLen = $this.attr('data-pagenum') || 6,
                type = $this.attr('data-type'),
                $wrap = $this.siblings('.slide-down__wrap'),
                $listEle = $wrap.children('ul'),
                $list = $listEle.children();

            if ($list.length == 0) {
                var $nav = $this.closest('.detail-box').find('.nav-list__item')
                $nav.eq($this.parent().index() - 1).remove()
                $this.parent().remove()
            } else if ($list.children().length <= pageLen) {
                $this.remove()
            } else {
                $list.each(function (index, ele) {
                    if (index >= pageLen) {
                        $(ele).css('display', 'none')
                    }
                })
                $wrap.css({
                    'transition': 'height .8s ease',
                    'overflow': 'hidden'
                })
            }



        })
        return this
    }

    checkList() {
        $('.detail-box').each(function (i, ele) {
            if ($(ele).children().length == 1) {
                $(ele).remove()
            }
        })
        return this
    }

    loadmore() {
        this.$loadMoreBtn.click(function () {
            var $this = $(this),
                pageLen = parseInt($this.attr('data-pagenum')) || 6,
                type = $this.attr('data-type'),
                $wrap = $this.siblings('.slide-down__wrap'),
                $listEle = $wrap.children('ul'),
                $list = $listEle.children(":hidden");

            $wrap.css({
                'height': `${$listEle.height()}px`,
            })
            if ($list.length <= pageLen) {
                $list.show()
                $this.remove()

            } else {
                $list.each(function (index, ele) {
                    if (index < pageLen) {
                        $(ele).show()
                    } else {
                        return
                    }
                })
            }
            $wrap.css('height', `${$listEle.height()}px`)


        })
        return this
    }
}

$(() => {
    new LoadMore('.click-to-showmore')
    new pageGameDetail()
})

window.onload = function () {

}


