// 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 GameData from './GameData';
import HttpUtil from './crypto/HttpUtil';
import { GameTool } from './tool/GameTool';
import { Notification } from './tool/Notification';

const {ccclass, property} = cc._decorator;

@ccclass
export default class GuideManager  extends cc.Component {

    @property(cc.Camera)
    Camera: cc.Camera = null;

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

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

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

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

    @property(cc.Label)
    time: cc.Label = null;

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

    @property([cc.Prefab])
    blockPrefab : Array<cc.Prefab> = [];

    

    round: number;      //回合
    level: number;      //回合
    static _instance: any;
    blockArray: any;
    cameraMove:boolean
    countHeight:number;
    countTime: number;
    over: boolean;
    begin: boolean;
    interfere: boolean;
    oldSpeed: number;      //上一回合速度
    score: number;      //总得分
    difficultyMax:number; //最高难度系数
    // LIFE-CYCLE CALLBACKS:



    onLoad () {
        // 示例使用
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
        GameData._instance.GM_INFO.probation = true;
    }

    protected onEnable(): void {
        Notification.on("createBlock",this.createBlock,this);
        Notification.on("addScore",this.addScore,this);
        Notification.on("createCrackBlock",this.createCrackBlock,this);
        Notification.on("showGround",this.showGround,this);
        Notification.on("death",this.getDeath,this);
        Notification.on("jump",this.jump,this);
        
    }

    protected onDestroy(): void {
        Notification.off("createBlock",this.createBlock);
        Notification.off("addScore",this.addScore);
        Notification.off("createBlock",this.createCrackBlock);
        Notification.off("death",this.getDeath);
        Notification.off("jump",this.jump);
        Notification.off("showGround",this.showGround);
    }

    init(){
        this.score = 0;
        this.oldSpeed = 10;
        this.begin = true;
        this.over = false;
        this.interfere = false;
        this.round = 0;
        this.level = 0;
        GameData._instance.GM_INFO.levelMax = 1;
        GameData._instance.GM_INFO.difficultyMax = 1;
        GameData._instance.GM_INFO.life = 3;
        this.blockArray = [];
        this.cameraMove = false;
        this.countTime = 12;
        this.topUI.getChildByName("xin1").active = true;
        this.topUI.getChildByName("xin2").active = true;
        this.topUI.getChildByName("xin3").active = true;
        this.topUI.getChildByName("star1").active = false;
        this.topUI.getChildByName("star2").active = false;
        this.topUI.getChildByName("star3").active = false;
        GameData._instance.LEVEL_init();

    }

    start () {
        // this.node.on(cc.Node.EventType.TOUCH_START, this.jump, this);
        this.fit();
        this.init();
        this.startGuide();
        this.startGame();
    }

    //根据是否全面屏，做独立适配方面
    fit(){
        this.node.getChildByName("bg2").y = -2167.675;
        var jg = this.setFit();
        if(!jg){
            this.node.getChildByName("bg2").y = -2100;
        }
    }
    //判断全面屏
    getSetScreenResolutionFlag () {
        let size = cc.winSize;
        let width = size.width;
        let height = size.height;
        if ((height / width) > (16.2 / 9)) return false;
        return true;
    }
    //判断全面屏适配
    setFit () {
        let flag = this.getSetScreenResolutionFlag();
        if (flag) {
        } else {
        }
        return flag;
    }

    startGuide(){
        //打开卷
        let left = this.Tip.getChildByName("left");
        let right = this.Tip.getChildByName("right");
        let tip = this.Tip.getChildByName("tip");
        left.width = 0; right.width = 0;tip.opacity = 0;
        cc.tween(left)
        .to(0.25,{width:348})
        .start();
        cc.tween(right)
        .to(0.25,{width:348})
        .start();
        cc.tween(tip)
        .delay(0.15)
        .to(0.15,{opacity:255})
        .start();
    }

    showGround(tempY){
        setTimeout(() => {
            this.Ground.active = true;
            this.Ground.y = tempY;
        }, 500);
    }

    jump(){
        this.Player.getComponent("Player").jump();
    }

    startGame(){
        this.createBlock();
    }

    addScore(score){

    }

    createBlock(){
        if(this.over == false && this.begin == true){
            this.Ground.active = false;
            var num = Math.floor(Math.random()*12);
            if(this.round <= 10 && num == 5){
                num = Math.floor(Math.random()*6 + 6);
            }
            // num = 2;
            var block = cc.instantiate(this.blockPrefab[num]);
            let height = this.Player.getComponent("Player").basicHeight + GameData._instance.GM_INFO.blockMin;
            if(num > GameData._instance.GM_INFO.blockScale){
                height += block.height + GameData._instance.GM_INFO.blockMax;
                GameData._instance.CLICK_DATA.height = true;
            }
    
            if(height >= -120 && this.cameraMove == false){
                this.countHeight = this.Player.getComponent("Player").basicHeight - this.Camera.node.y;
                this.cameraMove = true;
            }
            block.setPosition(cc.v2(this.round%2==0?500:-500,height));
            block.parent = this.node.getChildByName("Block");
            this.round += 1;
            this.level += 1;
            if(this.round > GameData._instance.GM_INFO.levelMax) GameData._instance.GM_INFO.levelMax = this.round;
            GameData._instance.CLICK_DATA.level = this.round;
            GameData._instance.CLICK_DATA.round = this.level;
            GameData._instance.LEVEL_INFO.layer += GameData._instance.LEVEL_INFO.layerAdd;
            GameData._instance.LEVEL_INFO.speed += GameData._instance.LEVEL_INFO.addSpeed;

            if(GameData._instance.LEVEL_INFO.reduceSpeed <= 100)
                GameData._instance.LEVEL_INFO.reduceSpeed += GameData._instance.LEVEL_INFO.jiansu;
            if(this.round == GameData._instance.LEVEL_INFO.round1) GameData._instance.LEVEL_INFO.jiansu = 2;
            else if(this.round == GameData._instance.LEVEL_INFO.round2) GameData._instance.LEVEL_INFO.jiansu = 1;
            else if(this.round == GameData._instance.LEVEL_INFO.round3) GameData._instance.LEVEL_INFO.jiansu = 0.5;
        }
    }

    //虚假碎裂块
    createCrackBlock(){
        this.interfere = true;
        GameData._instance.CLICK_DATA.fake = this.interfere;
        if(this.round > 1) this.destroyBlock();
        if(this.over == false && this.begin == true){
            var block = cc.instantiate(this.blockPrefab[12]);
            let height = this.Player.getComponent("Player").basicHeight;
            if(GameData._instance.CLICK_DATA.height == true){
                height += block.height/2;
            }
            block.setPosition(cc.v2(this.round%2==0?500:-500,height));
            block.parent = this.node.getChildByName("CrackBlock");
        }
    }
    
    destroyBlock(){
        if(this.node){
            if(this.node.getChildByName("CrackBlock")){
                if(this.node.getChildByName("CrackBlock").children){
                    var block2 = this.node.getChildByName("CrackBlock").children;
                    for(let i=0; i<block2.length; i++){
                        if(block2[i]){
                            block2[i].getComponent("Block").hide();
                        }
                    }
                }
            }
        }
    }

    xinAction(){

    }
    //死亡
    getDeath(){
        this.getDevive(1);
    }
    //复活
    getDevive(time){
        this.round = 0;
        GameData._instance.LEVEL_init();
        if(this.topUI.getChildByName("star3").active == true){
            // GameData._instance.LEVEL_INFO.scoreMax = 150000;
        }
        this.interfere = false;
        GameData._instance.CLICK_DATA.fake = this.interfere;
        var block = this.node.getChildByName("Block").children;
        for(let i=0; i<block.length; i++){
            if(block[i]){
                block[i].getComponent("Block").hide();
            }
        }
        var block2 = this.node.getChildByName("CrackBlock").children;
        for(let i=0; i<block2.length; i++){
            if(block2[i]){
                block2[i].getComponent("Block").hide();
            }
        }
        // this.blockArray = [];
        let actionTime = 0.5;
        if(this.Player.y > 500){
            actionTime = (this.Player.y + 500)/1000*actionTime;
        }
        cc.tween(this.Player)
        .to(actionTime,{position:cc.v3(0,-500,0)})
        .call(()=>{
            this.Player.getComponent("Player").xinAction();
        })
        .delay(0.5)
        .call(() =>{
            this.xinAction();
            this.Player.getComponent("Player").init();
            if(time > 1){
                this.Xin.parent.active = true;
                this.Xin.getComponent(cc.Sprite).fillRange = 0;
                cc.tween(this.Xin.getComponent(cc.Sprite))
                .to(time,{fillRange:1})
                .delay(0.3)
                .call(() =>{
                    this.Xin.parent.active = false;
                })
                .delay(0)
                .call(() =>{
                    this.createBlock();
                })
                .start();
            }
            else{
                setTimeout(() => {
                    this.createBlock();
                }, (time+0.5)*1000);
            }
        })
        .start();
    }

    // update (dt) {}
}
