// 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: require("Base"),

    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;
        //     }
        // },
        back: {
            default: null,
            type: cc.Sprite
        },
        detail: {
            default: null,
            type: cc.Sprite
        },
        popup_prefab: {
            default: null,
            type: cc.Prefab
        },
        popup_layout: {
            default: null,
            type: cc.Layout
        },
        tab_layout: {
            default: null,
            type: cc.Layout
        },
        nav_prefab: {
            default: null,
            type: cc.Prefab
        },
        video_prefab: {
            default: null,
            type: cc.Prefab
        }
        // videoplayer: cc.VideoPlayer
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // console.log(this.back.node)
        setViewTouch(
            this.back,
            null,
            function() {
              this.node.destroy();
            },
            null,
            this
        );
        // this.detailInit();
        setViewTouch(
            this.detail,
            null,
            function() {
              this.showDetail()
            },
            null,
            this
        );
        this.tabInit()
        // setViewTouch(
        //     this.shu,
        //     null,
        //     function() {
        //       var url = 'HelloWorld.png';
        //       var _this = this; 
        //       cc.loader.loadRes(url, cc.SpriteFrame, function(err, spriteFrame) {
        //         if (err) {
        //             cc.error(err.message || err);
        //             return;
        //         }
        //         _this.shu.spriteFrame = spriteFrame;
        //       });
        //     },
        //     null,
        //     this
        // );
        // this.videoplayer.node.on('ready-to-play', this.callback, this);
    },

    tabInit: function () {
        var tabContent = cc.instantiate(this.nav_prefab);
        this.tab_layout.node.addChild(tabContent);
        var video = cc.instantiate(this.video_prefab);
        for (let i = 0; i < 5; i++) {
           var courseware = cc.instantiate(this.popup_prefab);
           this.popup_layout.node.addChild(courseware);
           var length =  this.popup_layout.node.childrenCount;
            // courseware.getComponent("课件icon").kejian = this;
            var temp = this;
            courseware.getComponent("tabItem").setClickFunction(function(item) {
                for(var index = 0; index < length; index++) {
                    temp.popup_layout.node.children[index].getComponent("tabItem").shu.enabled = true
                    tabContent.getComponent("nav")['nav'+ (index+1)].enabled = false
                }
                item.target.getComponent("tabItem").shu.enabled = false
                tabContent.getComponent("nav")['nav'+ (i+1)].enabled = true
                if(i == 1) {
                    tabContent.getComponent("nav").node.addChild(video)
                }else if(tabContent.getComponent("nav").node.childrenCount == 7) {
                    tabContent.getComponent("nav").node.removeChild(video)
                }
            });
        }
    },
    // detailInit () {
    //     console.log(1111, cc.instantiate(this.detail))
    // }, 
    // callback: function (event) {
    //     console.log(event)
    //    //这里的 event 是一个 EventCustom 对象，你可以通过 event.detail 获取 VideoPlayer 组件
    //    var videoplayer = event.detail;
    //    //do whatever you want with videoplayer
    //    //另外，注意这种方式注册的事件，也无法传递 customEventData
    // },

    start () {

    },
    showDetail: function() {
        this.curriculum_window.showDetail();
    }
    // update (dt) {},
});
