// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // console.log("现在 我的属性是",this.node);
        this._init();
    },
    _init : function(){
        this.Canvas = cc.find("Canvas/UICamera");
        this.helpPrefab = [
            "prefab/task/taskPrefab3",
            "prefab/help/helpPrefab1",
            "prefab/task/taskPrefab4",
            "prefab/task/taskPrefab6",
            "prefab/task/taskPrefab7",
            "prefab/task/taskPrefab8",
            "prefab/help/helpPrefab6",
            "prefab/task/taskPrefab11",
            "prefab/task/taskPrefab12",
            "prefab/task/taskPrefab13",
            "prefab/task/taskPrefab13",
        ]
        this.curr = 0;

        this.close = UtilsPrefabs.getNode("close",this.node);
        this.listview = UtilsPrefabs.getNode("listview",this.node);
        this.layoutbutton = UtilsPrefabs.getNode("layoutbutton",this.node);


        this.up = UtilsPrefabs.getNode("up",this.layoutbutton);
        this.down = UtilsPrefabs.getNode("down",this.layoutbutton);


        for (var i = 0; i < this.listview.children.length; i++) {
            this.listview.children[i].active = false;
        }
        this.listview.children[this.curr].active = true;

        this.buttonHelp();


        this.isShowButton();


        this.onCallback();



    },
    buttonHelp : function(){
        this.arrbuttonHelp = [];
        for (var i = 0; i < this.listview.children.length; i++) {
           var childrens =  this.listview.children[i];
            for (var y = 0; y < childrens.children.length; y++) {
               var button =  childrens.children[y]
                    .getChildByName("New Node")
                    .getChildByName("guide")

                button.itemPosition = this.arrbuttonHelp.length;
                this.arrbuttonHelp.push(button);
            }

        }
        // console.log("是什么呢长度",this.arrbuttonHelp);
        for (var i = 0; i < this.arrbuttonHelp.length; i++) {
            var button =  this.arrbuttonHelp[i];

            UtilsPrefabs.setOn(button,function (e) {
                // this.setOnCallback(this.arrbuttonHelp[e.target.itemPosition]);
                this.setOnCallback(e.target.itemPosition);
            }.bind(this));
        }

    },
    //点了什么
    setOnCallback : function(count){


        if (count == 10) {

            dialogmanager.init(task.Canvas,function () {
                dialogmanager.creatorEnd();
            }.bind(this));
            dialogmanager.setOnCloseDialog(function () {
            }.bind(this));
            return;
        }

        // console.log("加了吗",count);
        task.addTaskTipsNoArr(this.node, this.helpPrefab[count], 0, 0, "videoTutorialCreatorBtn", function (nodePrefabs) {
            nodePrefabs.parent = this.node;

            if (count ==9 ) {

                task.addListView(nodePrefabs,function () {
                    task.removeNode(nodePrefabs);
                }.bind(this));
                return;
            }

            if (count<this.arrbuttonHelp.length-1) {
                UtilsPrefabs.setOn(nodePrefabs.getChildByName("SureBtn"),function () {
                    task.removeNode(nodePrefabs);
                }.bind(this))
            }

        }.bind(this));



    },



    onCallback : function(){
        UtilsPrefabs.setOff(this.up);
        UtilsPrefabs.setOff(this.down);

        UtilsPrefabs.setOn(this.up,function () {
            this.addCurr(false);
        }.bind(this));
        UtilsPrefabs.setOn(this.down,function () {
            this.addCurr(true);
        }.bind(this));
    },

    addCurr : function(b){
        // console.log("增加 减少",b,"当前索引",this.curr,"总长度是",this.listview.children.length);
        if (b) {
            if (this.curr < this.listview.children.length-1) {
                this.curr++;
            }
        }else{
            if (this.curr > 0) {
                this.curr--;
            }
        }
        for (var i = 0; i < this.listview.children.length; i++) {
            this.listview.children[i].active = false;
        }
        this.listview.children[this.curr].active = true;
        this.isShowButton();
    },


    isShowButton : function(){
        if (this.curr == 0) {
            this.up.active = false;
            this.down.active = true;
        }else if (this.curr == this.listview.children.length-1) {
            this.up.active = true;
            this.down.active = false;
        }else{
            this.up.active = true;
            this.down.active = true;
        }
    },


    start () {

    },

    // update (dt) {},
});
