import GameMgr from "../../FrameWork/Mgr/GameMgr";
import User from "../../FrameWork/User/User";
import Common5 from "../../Platform/th/Common5";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import { GameStoryConfigData } from "./GameStoryConfig";
import SingleItem from "./SingleItem";


const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelView extends cc.Component {

    @property(cc.Node)
    contentNode: cc.Node = null;

    @property(cc.Prefab)
    singleItemPrefab: cc.Prefab = null;


    @property(cc.Node)
    endLevelIcon: cc.Node = null;
    @property(cc.Node)
    endLevelTitle: cc.Node = null;
    @property(cc.Node)
    endLevelPoint: cc.Node = null;
    @property(sp.Skeleton)
    tittleSpine: sp.Skeleton = null;

    SinglePageMaxNum: number = 15;
    curPageIndex: number = 0;

    endLevelConfig = null
    endLevelState = 0; //0 未开发 1 未解锁 2 已通关 3 已解锁
    // onLoad () {}

    start() {
        this.initView();
    }

    // 初始化实例
    initView() {
        let curGameLevelIndex = User.getCurGameLevelIndex();

        let endIndex = (this.curPageIndex + 1) * this.SinglePageMaxNum - 1;
        let levelList: number[] = [];
        let startIndex = this.curPageIndex * this.SinglePageMaxNum;
        for (let i = 0; i < this.SinglePageMaxNum - 1; i++) {
            levelList.push(startIndex + i);
        }

        // if (User.getFirstStepIndex() == 0) {
        this.loadLevelItem(endIndex - 1, levelList);
        // } else if (curGameLevelIndex < endIndex) {
        //     this.loadLevelItem(curGameLevelIndex, levelList);
        // } else {
        //     this.loadLevelItem(endIndex - 1, levelList);
        // }

        this.initEndLevel(curGameLevelIndex, endIndex);
        this.locateNewLevel(curGameLevelIndex);
    }

    // 加载关卡图标
    loadLevelItem(curLevelIndex: number, levelList: number[], addIndex: number = 0) {
        let index = levelList.indexOf(curLevelIndex);
        let curConfig = GameStoryConfigData[curLevelIndex];
        let parentNode = this.contentNode.getChildByName('node_' + (index + 1));
        if (parentNode) {
            parentNode.removeAllChildren();
            let item = cc.instantiate(this.singleItemPrefab);
            item.active = true
            item.parent = parentNode
            item.getComponent(SingleItem).initView(curConfig);
        }

        do {
            addIndex *= -1;
            if (addIndex > 30) {
                addIndex = 999;
                break;
            } else if (addIndex < 0) {
                addIndex--;
            } else {
                addIndex++;
            }
            curLevelIndex += addIndex;
        } while (levelList.indexOf(curLevelIndex) == -1);

        if (addIndex != 999) {
            this.scheduleOnce(() => {
                this.loadLevelItem(curLevelIndex, levelList, addIndex);
            }, 0);
        }
    }

    //初始化结束关卡
    initEndLevel(curGameLevelIndex: number, endIndex: number) {
        this.endLevelConfig = GameStoryConfigData[endIndex];

        //Common5.getSpriteFrameFromBundle("FirstSelectScene", "picture/zoomLevel/" + this.endLevelConfig.titleIconUrl, this.endLevelTitle.getComponent(cc.Sprite));

        for (const mask of this.endLevelPoint.children) {
            mask.active = false;
        }

        if (!this.endLevelConfig.isCanPlay) {
            this.endLevelPoint.children[2].active = true;
            this.endLevelState = 0;
            return;
        }

        if (endIndex > curGameLevelIndex) {
            this.endLevelPoint.children[2].active = true;
            this.endLevelState = 1;
        } else if (endIndex < curGameLevelIndex) {
            this.endLevelPoint.children[0].active = true;
            this.endLevelState = 2;
        } else {
            this.endLevelPoint.children[1].active = true;
            this.endLevelState = 3;
        }
    }

    // 定位最新关卡
    locateNewLevel(curLevelIndex: number) {
        let view = this.contentNode.parent;
        // if (User.getFirstStepIndex() == 0) {
        let sy = view.height / 2;
        let dy = this.contentNode.y - this.contentNode.getChildByName('node_1').y - view.height + 50;
        this.node.getChildByName("横断虚线").active = false;
        cc.tween(this.contentNode)
            .set({ y: sy })
            .delay(1.7)
            .call(() => {
                this.node.getChildByName("横断虚线").active = true;
                this.node.getChildByName('ScrollView').getComponent(cc.ScrollView).enabled = true;
                this.tittleSpine.node.active = true;
                //this.endLevelTitle.active = false;
                this.tittleSpine.setAnimation(0, "文字", false);
            })
            .to(1.5, { y: dy })
            .call(() => {
                this.node.getChildByName('ScrollView').getComponent(cc.ScrollView).enabled = true;
                this.tittleSpine.node.active = false;
                //this.endLevelTitle.active = true;
            })
            .start();
        // } else {
        //     let lastIndex = curLevelIndex % this.SinglePageMaxNum + 1;
        //     lastIndex = lastIndex > this.SinglePageMaxNum - 2 ? this.SinglePageMaxNum - 2 : lastIndex;
        //     let parentNode = this.contentNode.getChildByName('node_' + lastIndex);
        //     let dy = this.contentNode.y - parentNode.y - view.height + 200;
        //     dy = dy < view.height / 2 ? view.height / 2 : dy;
        //     this.contentNode.y = dy;
        // }
    }

    scrollViewEvent(event: cc.ScrollView) {
        let view = event.content.parent;
        if (event.content.y <= view.height / 2 + 5) {
            this.node.getChildByName("横断虚线").active = false;
        } else {
            this.node.getChildByName("横断虚线").active = true;
        }
    }

    onEndLevelClick() {
        // //Common5.playEffect("sound/按键点击");
        if (this.endLevelState == 0) {
            PrefabManage.showTextTips('正在努力开发中!');
            return;
        } else if (this.endLevelState == 1) {
            PrefabManage.showTextTips('未来还没来,一切皆可能');
            return;
        } else if (this.endLevelState == 2) {
            PrefabManage.showTextTips('过去已过去,计较也无用');
            return;
        }

        Common5.selectGameInfo = this.endLevelConfig;
        // GameMgr.getInstance_custom().onLoadToGameScene_custom();
    }

    onBtnClickClose() {
        //Common5.playEffect("sound/按键点击");
        this.node.parent.removeAllChildren()
        PrefabManage.loadPrefabByType(GameType.MainHall)
    }

    playguide(){
        let node = this.node.getChildByName("回到首页")
        this.guideView([node])
    }

    guideView(nodeArray, opacity = -1) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, this.node, (prefab) => {
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if (opacity != -1) {
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }
}