seajs.use(['arale/switchable/1.0.2/slide','arale/dialog/1.3.1/dialog'], function(Slide,Dialog) {

  var taskTpl = __inline('../../tpl/simulatorTask.tmpl');
  var heziType = commonUtil.getParam("hezi");

  var M = {
        ver : 1,
        os : 5,
        channel : "web",
        domain : "http://ms-api.duowan.com",
        recommendCache : {},
        typeId : 1,
        sortType : 1,
        recommendCache : {},
        //banner图
        getBanner : function(){
            var url = M.domain+"/home/banner.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                V.renderBanner(ret);
              },
              error: function(ret){
                M.getBanner();
              }
            });
        },
        //获取所有游戏类别
        getAllCategory : function(){
            var url = M.domain+"/category/all.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                V.renderAllCategory(ret.data);
                C.categoryLen = ret.data.list.length;
                M.getRecommendByCategory(ret.data.list[0]["id"]);
              },
              error: function(ret){
                M.getAllCategory();
              }
            });
        },
        //获取编辑推荐
        getRecommend : function(){
            var url = M.domain+"/home/lolRank.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os,
              uid: M.uid,
              token: M.token
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                V.renderDownloadRank(ret);
              },
              error: function(ret){
                M.getDownloadRank();
              }
            });
        },
        //获取游戏下载排行
        getDownloadRank : function(){
            var url = M.domain+"/home/downloadRank.do";
            var data = {
              channel: M.channel,
              ver: M.ver,
              os: M.os,
              uid: M.uid,
              token: M.token
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                V.renderDownloadRank(ret);
              },
              error: function(ret){
                M.getDownloadRank();
              }
            });
        },
        //由推荐类型获取推荐游戏列表
        getRecommendByCategory : function(categoryId){
            var url = M.domain+"/mobileGame/byCategory.do";
            var data = {
              categoryId: categoryId,
              sortType: 1,
              channel: M.channel,
              ver: M.ver,
              os: M.os
            };

            var recommendType = "category"+categoryId;

            if (M.recommendCache.hasOwnProperty(recommendType)) {
                V.renderRecommender(M.recommendCache[recommendType]);
              return;
            }

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                M.recommendCache[recommendType] = ret;
                V.renderRecommender(ret);
              },
              error: function(ret){
                M.getRecommendByCategory(categoryId);
              }
            });
        },
        //获取当前任务
        getCurrentTask : function(){
            var url = M.domain+"/task/getCurrent.do";
            var data = {
              yyuid : commonUtil.getCookie("yyuid"),
              channel: M.channel,
              ver: M.ver,
              os: M.os
            };

            $.ajax({
              url: url,
              dataType: 'jsonp',
              data: data,
              success: function(ret){
                if (ret.code==0) {
                  var data = ret.data;
                  
                  var acceptLink = 'lolbox://simulator_task?apkUrl='+data.game["apkUrl"]+'&icon='+data.game["icon"]+'id'+data.game["id"]+'&name='+data.game["name"]+'&packageName='+data.game["packageName"]
                            +'&versionCode='+data.game["versionCode"]+'&qCoin='+data.qCoin+'&requireMinutes='+data.requireMinutes+'&finished='+data.finished;

                  data.acceptLink = acceptLink;
                  data.isLogined = true;

                  V.renderTask(data);
                }
              },
              error: function(ret){
                M.getCurrentTask();
              }
            });
        }
    }

    var V = {
      init : function(){
        commonUtil.initHezi();
        M.os = commonUtil.checkPlatform();
        M.getBanner();
        // M.getDownloadRank();
        M.getRecommend();
        M.getAllCategory();

        V.initTask();
      },
      renderBanner : function(ret){
        var bannerTpl = __inline('../../tpl/simulatorBannar.tmpl');
        var dom = bannerTpl({
          data : ret.data.list
        });

        $("#bannerWrap").html(dom);

        if(!ret.data.list){
          return;
        }
        var slide1 = new Slide({
            element: '[data-role="slide-banner"]',
            classPrefix: null,
            activeTriggerClass: 'is-active',
            autoplay: true,
            effect: 'scrollx'
        }).render();
      },
      renderTask : function(data){
        
        var dom = taskTpl(data);

        $("#taskWrap").html(dom);
      },
      renderAllCategory : function(data){
        var list = data.list;
        var len = list.length;
        var dom = "";

        for (var i=0; i<len; i++){
          if (i==0) {
            dom += '<li data-id="'+list[i].id+'" class="active">'+list[i].categoryName+'</li>';
          } else {
            dom += '<li data-id="'+list[i].id+'">'+list[i].categoryName+'</li>';
          }
        }

        $("#categoryWrap").append(dom);
      },
      renderRecommenderType : function(ret){
        var list = ret.data.list;
        var len = list.length;
        var dom = "";

        for (var i=0; i<len; i++){
          dom += '<option value="'+list[i]["id"]+'">'+list[i]["typeName"]+'</option>';
        }

        $("#recommendTypeWrap").html(dom);
        M.getRecommendByCT($("#recommendTypeWrap").val());
      },
      renderRecommender : function(ret){
        var recommendTpl = __inline('../../tpl/simulatorRecommend.tmpl');
        
        var dom = recommendTpl({
          data : ret.data.list
        });

        $("#recommendWrap").html(dom);
      },
      renderDownloadRank : function(ret){
        var rankTpl = __inline('../../tpl/simulatorDlRank.tmpl');
        var dom = rankTpl({
          data : ret.data.list
        });

        $("#dlRankWrap").html(dom);
      },
      initBanner : function(){
        var slide = new Slide({
            element: '[data-role="slide-banner"]',
            classPrefix: null,
            activeTriggerClass: 'is-active',
            autoplay: false,
            effect: 'scrollx'
        }).render();
      },
      popUp : function(){
        var popTpl = __inline('../../tpl/simulatorDialog.tmpl');
        var dom = popTpl();

        var pop = new Dialog({ 
            contentType:4,
            content: dom,
            width:520,
            height: 320,
            classPrefix:"pop-center"
          }).show();

          //弹框
          $(".pop-center .btn-go").click(function(){
            $(this).parent().fadeOut().siblings().fadeIn();
          });

          $(".pop-center .btn-open").click(function(){
            pop.hide();
          });
      },
      //初始化任务:天刀盒子用网页登录
      initTask : function(){

          if (dwUDBProxy.isLogin()) {
            M.getCurrentTask();
          } else {
            var data = {
              isLogined : false,
              isYYLogin : heziType=="td" ? false : true,
              game : {
                name : "指定游戏"
              }
            }

            V.renderTask(data);
          }
      }
    }

    var C = {
      categoryLen : 0, //推荐游戏类型长度
      categorySildeNum : 5, //推荐游戏每次滚动个数
      categoryCurrent : 0,
      init : function(){

        //排行榜
        $("#dlRankWrap").on("mousemove",".item-rank",function(){
          $(this).addClass("active").siblings().removeClass("active");
        });

        //推荐位游戏
        $("#categoryWrap").on("click","li",function(){
          var $this = $(this);
          var id = $this.attr("data-id");

          $this.addClass("active").siblings().removeClass("active");

          M.getRecommendByCategory(id);
        });

        var $btnPrev = $(".wrap-type .btn-prev");
        var $btnNext = $(".wrap-type .btn-next");

        $(".wrap-type").on("click",".btn-next,.btn-prev",function(){
          var $this = $(this);
          var totalPage = Math.ceil(C.categoryLen / C.categorySildeNum);

          if (C.categoryLen<=0) {
            return;
          }

          if (!$this.hasClass("active")) {
            return;
          }

          if($this.hasClass("btn-next")) {
            ++C.categoryCurrent;
          } else {
            --C.categoryCurrent;
          }

          if (C.categoryCurrent > 0) {
            $btnPrev.addClass("active");
          } else {
            $btnPrev.removeClass("active");
          }

          if (C.categoryCurrent < totalPage-1) {
            $btnNext.addClass("active");
          } else {
            $btnNext.removeClass("active");
          }

          $("#categoryWrap li").each(function(i,e){
            if (i>=C.categoryCurrent*C.categorySildeNum && i<(C.categoryCurrent+1)*C.categorySildeNum) {
              $(e).show();
            } else {
              $(e).hide();
            }
          });
        });

        //任务领取奖励-普通网页登录
        $(document).on("click",".btn-accept.is-noLogin",function(){
            dwUDBProxy.login();
        });
      }
    }

    function initPop() {
      var timeout = setTimeout(function(){
        if(window.doneFlag) {
          if (window.popStatus==1) {
            V.popUp();
          }
        } else {
          initPop();
        }
      },200);
    }

    C.init();
    V.init();
    initPop();
});
 
