import AudioManager from "./module/Music/AudioManager";

// 主游戏控制类
const {ccclass, property} = cc._decorator;
@ccclass
export default class GuideManager extends cc.Component {

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

    @property(cc.Node)
    Btn_New: cc.Node = null;
    @property(cc.Node)
    Btn_Hear: cc.Node = null;
    @property(cc.Node)
    Btn_See: cc.Node = null;
    @property(cc.Node)
    Btn_Firewood: cc.Node = null;
    @property(cc.Node)

    
    Plant: cc.Node = null;
    voiceSpriteFrame: cc.SpriteFrame;     //是否可点击

    needleTween: cc.Tween<cc.Sprite>;
    func: () => void;

    custom:number;

    now_Food: string;       //当前关卡名字信息
    customData: { item: string; type: number; right: number; }[];
    choice: number;

    onLoad () {
        this.custom = 0;
    }

    start () {
        this.fit();
    }

    fit(){
        var jg = cc.fx.GameTool.setFit();
        if(!jg){
            this.Btn_Firewood.y = -600;
        }
        this.Btn_Firewood.getComponent(cc.Button).interactable = true;
    }

    //初始化数据
    init(){
        this.voiceSpriteFrame = this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame;``
        this.node.getChildByName("jumpBtn").active = true;
        this.node.getChildByName("nextBtn").active = false;
        cc.tween(this.node.getChildByName("jumpBtn"))
        .to(0.35,{opacity:255})
        .start();
        let tip = cc.fx.tipType.tipOne;

        let label = this.node.getChildByName("tip1").getComponent(cc.Label);
        cc.fx.GameTool.typingAni(label,tip,this.startGame.bind(this),this);

        this.initData();
    }

    initData(){
        this.customData = [
            {
                "item": "putao",
                "type": 1,
                "right":1,
            },
            {
                "item": "taozi",
                "type": 2,
                "right":1,
            },
            {
                "item": "putao",
                "type": 2,
                "right":3,
            },
            {
                "item": "muchai",
                "type": 1,
                "right":1,
            },
            {
                "item": "taozi",
                "type": 1,
                "right":2,
            },
            {
                "item": "putao",
                "type": 1,
                "right":2,
            },
        ]
    }

    passVoice(){
        // cc.fx.AudioManager._instance.playMusicGame();
        let voice = this.node.getChildByName("voiceTip");
        voice.getChildByName("startBtn").getComponent(cc.Button).interactable = false;
        voice.getChildByName("playBtn").getComponent(cc.Button).interactable = false;
        cc.tween(voice)
        .to(0.5,{opacity:0})
        .call(()=>{
            voice.active = false;
        })
        .start();
        setTimeout(() => {
            this.init();
        }, 1000);
    }

    playVoice(){
        cc.fx.AudioManager._instance.playEffect("taozi_audio",null);
    }

    jumpClick(){
        cc.director.loadScene("GameScene");
    }

    nextClick(){
        //第一步
        this.node.getChildByName("nextBtn").active = false;
        let label = this.node.getChildByName("tip1").getComponent(cc.Label);
  
        if(this.custom == 0){
            cc.tween(this.node.getChildByName("tip1"))
            .to(0.2,{opacity:0})
            .call(() =>{
                label.string = "";
            })
            .to(0.1,{opacity:255})
            .call(() =>{ 
                let tip = cc.fx.tipType.tipTwo;
                cc.fx.GameTool.typingAni(label,tip,this.startGame.bind(this),this);
            })
            .start();
        }
        else if(this.custom == 1){
            this.node.getChildByName("nextBtn").active = false;
            this.node.getChildByName("jumpBtn").active = false;
            cc.tween(this.node.getChildByName("tip1"))
            .to(0.2,{opacity:0})
            .call(() =>{
                label.string = "";
                this.nextLevel();
            })
            .start();
        }
        this.custom += 1;
    }

    nextLevel(){
        this.choice = 0;
        this.Btn_Hear.active = true;
        this.Btn_New.active = true;
        this.Btn_See.active = true;
        this.Btn_Firewood.active = true;
        this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
        this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
        this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
        if(this.custom == 5){
            this.node.getChildByName("muchai").active = true;
            this.node.getChildByName("hand").active = true;
        }
        this.Btn_Firewood.getComponent(cc.Button).interactable = false;
        let name = this.customData[this.custom-2].item;
        if(this.customData[this.custom-2].type == 1){
            this.Plant.getChildByName("name").getComponent(cc.Label).string = cc.fx.GameTool.getFoodName(name);
            name = "prop_"+name;
            this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.FoodPlist["_spriteFrames"][name];
            this.now_Food = name;
            cc.tween(this.Plant)
            .to(0.2,{opacity:255})
            .call(()=>{
                this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = true;
                this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = true;
                this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = true;
                if(this.custom == 5){
                    this.Btn_Firewood.getComponent(cc.Button).interactable = true;
                }
            })
            .start();
        }
        else{
            this.Plant.getChildByName("name").getComponent(cc.Label).string = ""
            this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.voiceSpriteFrame;
            let audioName = name + "_audio";
            name = "audio"+ name;
            this.now_Food = name;
            cc.fx.AudioManager._instance.playEffect(audioName,null);
            cc.tween(this.Plant)
            .to(0.2,{opacity:255})
            .call(()=>{
                this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = true;
                this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = true;
                this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = true;
            })
            .start();
        }
        //关卡+1

    }

    //新植物按钮
    click_Btn(customData,data){
        //关掉计时器   处理逻辑之前，先行关掉按钮开关
        if(data == "Btn_New"){
            this.choice = 1;
            this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
        } 
        else if(data == "Btn_Hear"){
            this.choice = 2;
            this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
        } 
        else if(data == "Btn_See"){
            this.choice = 3;
            this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
        } 
        else if(data == "Btn_Firewood"){
            this.Btn_Firewood.getChildByName("fireBg").active = true;
            this.Btn_Firewood.getChildByName("fire").active = true;
            setTimeout(() => {
                if(this.Btn_Firewood){
                    this.Btn_Firewood.getChildByName("fireBg").active = false;
                    this.Btn_Firewood.getChildByName("fire").active = false;
                }
            }, 1050);
            console.log("正确点火");
            this.Btn_Firewood.getComponent(cc.Button).interactable = false;
            this.node.getChildByName("muchai").active = false;
            this.node.getChildByName("hand").active = false;
            return;
        } 
        let target = this[data];
        var label = this.Tip.getChildByName("content").getComponent(cc.Label);
        //正确
        if(this.choice == this.customData[this.custom-2].right){
            cc.fx.AudioManager._instance.playEffect("yes",null);
            this.Tip.stopAllActions();
            this.Tip.active = false;
            this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
            this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
            this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
            this.Btn_Firewood.getComponent(cc.Button).interactable = false;
            target.getChildByName("err").active = false;
            target.getChildByName("correct").active = true;
            setTimeout(() => {
                this.Btn_New.getChildByName("nomal").active = true;
                this.Btn_New.getChildByName("err").active = false;
                this.Btn_New.getChildByName("correct").active = false;
                this.Btn_Hear.getChildByName("nomal").active = true;
                this.Btn_Hear.getChildByName("err").active = false;
                this.Btn_Hear.getChildByName("correct").active = false;
                this.Btn_See.getChildByName("nomal").active = true;
                this.Btn_See.getChildByName("err").active = false;
                this.Btn_See.getChildByName("correct").active = false;
                this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
                this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
                this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
                this.custom += 1;
                if(this.custom >= 8){
                    this.node.getChildByName("againBtn").active = true;
                    this.node.getChildByName("startBtn").active = true;
                    this.Btn_Hear.active = false;
                    this.Btn_New.active = false;
                    this.Btn_See.active = false;
                    this.Btn_Firewood.active = false;
                } 
                else this.nextLevel();
            }, 1000);
        }
        else{
            cc.fx.AudioManager._instance.playEffect("yes",null);
            var tipTemp = '葡萄刚才出现过呢'
            this.Tip.active = true;
            target.getChildByName("err").active = true;
            target.getChildByName("correct").active = false;

            switch((this.custom-2)){
                case 0:
                    tipTemp = "这是这局游戏第一次出现葡萄";
                    break;
                case 1:
                    tipTemp = "这是这局游戏第一次出现桃子";
                    break;
                case 2:
                    if(this.choice == 2) tipTemp = '上次遇到葡萄时，似乎不是听到的吧';
                    break;
                case 3:
                    tipTemp = "这是这局游戏第一次出现木柴";
                    break;
                case 4:
                    if(this.choice == 3) tipTemp ='上次遇到桃子时，似乎不是看到的吧';
                    else  tipTemp = '桃子刚才出现过呢'
                    break;
                case 5:
                    if(this.choice == 3) tipTemp = '之前确实看到过葡萄，但最近一次似乎不是看到的呢';
                    break;
            }
            label.string = tipTemp;
            this.Tip.opacity = 255;
            this.Tip.stopAllActions();
            var self = this;
            var action = cc.callFunc(function(){
                self.Tip.active = false;
            })
            this.Tip.runAction(cc.sequence(cc.delayTime(2),cc.fadeIn(0.5),action))
        }
    }

    //开始游戏
    startGame(){
        this.node.getChildByName("nextBtn").active = true;
        cc.tween(this.node.getChildByName("nextBtn"))
        .to(0.35,{opacity:255})
        .start();
    }

    againClick(){
        cc.director.loadScene("GuideScene");
    }

    

    onEnable () {
        // cc.fx.Notifications.on("clickSun", this.clickSun, this);
    }
    onDisable () {
        // cc.fx.Notifications.off("clickSun", this.clickSun);
    }
    update (dt) {
    }
}
