// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AppPlatform from "../../../FrameWork/Util/AppPlatform";
import Common5 from "../../../Platform/th/Common5";

import LevelSelectNew from "./levelSelectNew";

const {ccclass, property} = cc._decorator;

@ccclass
export default class levelPageNew extends cc.Component {

    cellCount:number = 0;
    pageIndex:number = 0;
    typeIndex:number = 0;
    @property(cc.Prefab)
    levelPrefab: cc.Prefab = null;
    @property(cc.Node)
    levelsPage: cc.Node = null;
    
    @property(cc.Label)
    pageIndexLabel: cc.Label = null;
    @property(cc.Node)
    left: cc.Node = null;
    @property(cc.Node)
    right: cc.Node = null;
    @property(cc.PageView)
    levelPageView: cc.PageView = null;
    onLoad() {
        
    }
    start () {
        // this.initPage();
        this.initPage();
        let index = 0
    }
    
    setCellCount(count){
        this.cellCount = count;
 
    }
    setLevelIndex(index){
        this.pageIndex = index;
    }
    setTypeIndex(index){
        this.typeIndex = index;        
        // console.log("settype"+index)
    }

    initPage(){
        let levelPage = null; 
        this.left.active = false;
        console.log(Common5.curWordGameType)
        console.log(Common5.gameConfig.GameAllType)
        this.pageIndexLabel.string = "1/"+   Math.ceil(Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels.length/6);
        for (let i = 0; i < Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels.length; i++){
            if (i%6 == 0){
                levelPage = cc.instantiate(this.levelsPage);
                levelPage.active = true;
                this.levelPageView.addPage(levelPage);
            }
            
            if (i < 6){                
                let level = Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels[i];
                let levelNode = cc.instantiate(this.levelPrefab);
                levelNode.getComponent(LevelSelectNew).setLevel(level,i);
                levelNode.getComponent(LevelSelectNew).setType(0);
                levelNode.getComponent(LevelSelectNew).updateUI()
                levelPage.addChild(levelNode);
            }
        }
        // this.levelPageView.scrollToPage(0,0.1);
        this.levelPageView.setCurrentPageIndex(0);
        this.node.getComponent(cc.Widget).updateAlignment();
    }
    protected onEnable(): void {
        // console.log("enable!")
        this.levelPageView.setCurrentPageIndex(this.levelPageView.getCurrentPageIndex());
    }
    leftClick(){
        if (this.levelPageView.getCurrentPageIndex() > 0){
            let allPages = this.levelPageView.getPages();
            this.addLevelToPage(allPages[this.levelPageView.getCurrentPageIndex()],this.levelPageView.getCurrentPageIndex()-1)
            this.levelPageView.scrollToPage(this.levelPageView.getCurrentPageIndex()-1,0.3);
            allPages[this.levelPageView.getCurrentPageIndex()].children.forEach((ele)=>{
                ele.active = false;
            })
            this.onPageClick();
        }
    }
    rightClick(){
        if (this.levelPageView.getCurrentPageIndex() < this.levelPageView.getPages().length-1){
            let allPages = this.levelPageView.getPages();
            this.addLevelToPage(allPages[this.levelPageView.getCurrentPageIndex()],this.levelPageView.getCurrentPageIndex()+1)
            this.levelPageView.scrollToPage(this.levelPageView.getCurrentPageIndex()+1,0.3);
            allPages[this.levelPageView.getCurrentPageIndex()].children.forEach((ele)=>{
                ele.active = false;
            })
            this.onPageClick();
        }
    }
    addLevelToPage(page,index){
        if (page.childrenCount  == 0){
            let levelPage = page;
            for (let i = index*6; i<Math.min(Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels.length,(index+1)*6); i++){
                let level = Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels[i];
                let levelNode = cc.instantiate(this.levelPrefab);
                levelNode.getComponent(LevelSelectNew).setLevel(level,i);
                levelNode.getComponent(LevelSelectNew).setType(0);
                levelNode.getComponent(LevelSelectNew).updateUI()
                levelPage.addChild(levelNode);
            }
        }
        else {
            page.children.forEach(element => {
                element.active = true;
            });
        }
    }
    updateUI(){
        // console.log('updateUI levelPageNew')
    }

    onPageClick(){
        // console.log(this.levelPageView.getCurrentPageIndex())

        let index = this.levelPageView.getCurrentPageIndex()
        // console.log(index)
        let allPages = this.levelPageView.getPages();
        this.addLevelToPage(allPages[index],index)
        this.pageIndexLabel.string = (this.levelPageView.getCurrentPageIndex()+1) + "/"+   Math.ceil(Common5.gameConfig.GameAllType[Common5.curWordGameType].Levels.length/6);
        if (this.levelPageView.getCurrentPageIndex() <= 0){
            this.left.active = false;
        }
        else {
            this.left.active = true;
            allPages[this.levelPageView.getCurrentPageIndex()-1].children.forEach((ele)=>{
                ele.active = false;
            })
        }
        console.log(this.levelPageView.getPages().length)
        if (this.levelPageView.getCurrentPageIndex() >= this.levelPageView.getPages().length-1){
            this.right.active = false;
        }
        else {
            this.right.active = true;
            allPages[this.levelPageView.getCurrentPageIndex()+1].children.forEach((ele)=>{
                ele.active = false;
            })
        }
    }


    setGameType(wordGameTypeImg){
    }


    // update (dt) {}
}