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

import DaDianScript from "../../../FrameWork/Base/DaDianScript";
import WordGameBaseComponent from "../../../FrameWork/Base/WordGameBaseComptent";
import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common from "../../../FrameWork/Util/Common";
import Common5 from "../../../Platform/th/Common5";
import Game from "../../../Scripts/Game";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BaoDanChaoFan extends WordGameBaseComponent {

    @property(cc.Node)
    chaofanSpine:cc.Node = null;  
    @property(cc.Node)
    renwuNode:cc.Node = null;  
    @property(cc.Node)
    qipaoNode:cc.Node = null;  

    
    // LIFE-CYCLE CALLBACKS:
    // onLoad () {}
    curIndex = 0

    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianArrayConfig: any[] = [];
    wrongAnswerConfig: string[] = [];
    answersDuihuaConfig: any[] = [];
    start () {
        super.start();
        DaDianScript.userEnterDaDian();

        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/BaoDanChaoFanConfig',(assest)=>{
            this.jsonData = assest.json
            this.initParameters();
           
        })

        EventMgr.onEvent_custom(ryw_Event.DirectTouchMoveCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);
        
        this.showQiPao(this.qipaoNode, '师傅,来份最臭的臭蛋炒饭',3)
    }

    normalTouchCheckCallback(targetNode){
        switch (targetNode.name) {
            case '榴莲':
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('榴莲').active = true
                this.renwuNode.getChildByName('榴莲').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.unlockLevel(3)
                this.endGameView()
                break;
            case '牛瘪火锅':
                this.unlockLevel(6)
           
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('牛瘪火锅').active = true
                this.renwuNode.getChildByName('牛瘪火锅').zIndex = -1//this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break

            case '猪大肠':
                this.unlockLevel(5)
             
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('猪大肠').active = true
                this.renwuNode.getChildByName('猪大肠').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break


            case '白菜':
                targetNode.active = true
                this.showQiPao(this.qipaoNode, '你敢拿这不臭的糊弄我',2)
         
                break
            case '米饭打开_':
                targetNode.active = true
                this.showQiPao(this.qipaoNode, '你敢拿这不臭的糊弄我',2)

                break
            case '臭米饭打开':
                this.unlockLevel(0)
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('米饭').active = true
                this.renwuNode.getChildByName('米饭').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break
            case '臭豆腐':
                this.unlockLevel(7)
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('臭豆腐1').active = true
                this.renwuNode.getChildByName('臭豆腐1').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break
            case '胡萝卜丝':
                targetNode.active = true
                this.showQiPao(this.qipaoNode, '你敢拿这不臭的糊弄我',2)
   
                break
            case '臭鸭蛋':
                this.unlockLevel(1) 
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('臭蛋').active = true
                this.renwuNode.getChildByName('臭蛋').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break
            case '酸笋':
                this.unlockLevel(2)
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('酸笋1').active = true
                this.renwuNode.getChildByName('酸笋1').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break

            case '鞋垫':
                this.unlockLevel(8)
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                
             
                this.renwuNode.getChildByName('臭鞋垫1').active = true
                this.renwuNode.getChildByName('臭鞋垫1').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break           
            case '鲱鱼罐头':
                this.unlockLevel(4)
                this.curIndex++
                if(this.curIndex>=9){
                    this.showQiPao(this.qipaoNode, '就是这个味,爱死了',2)
                }else{
                    this.showQiPao(this.qipaoNode, '不够臭!',1)
                }
                this.renwuNode.getChildByName('臭鱼').active = true
                this.renwuNode.getChildByName('臭鱼').zIndex = this.curIndex
                this.renwuNode.getChildByName('覆盖层').active = true
                this.renwuNode.getChildByName('覆盖层').zIndex = 100
                this.renwuNode.getChildByName('覆盖层').opacity = 100+this.curIndex*17
                this.endGameView()
                break             
            default:
                break;
        }

    }

    showQiPao(qipaoNode, str, delayTime=3, callFunc=null){
        qipaoNode.scale = 0
        qipaoNode.active = true
        qipaoNode.stopAllActions()
        qipaoNode.getChildByName("str").getComponent(cc.Label).string = str
        if (str) {
            Common5.playEffectCustom("baodanchaofan", "sound/"+str);
        }
        cc.tween(qipaoNode)
            .to(0.2,{scale:1.5})
            .delay(delayTime)

            .call(()=>{
                qipaoNode.active = false
                if(callFunc){
                    callFunc()
                }
            })
        .start()
    }


    unlockLevel(sublevel) {
        Game.ins.tipUnlock(sublevel)
    }
    
    initParameters(){
        this.titleArrayConfig = this.jsonData.titleArray;
        this.duihuaArrayConfig = this.jsonData.duihuaArray;
        this.chadianArrayConfig = this.jsonData.chadianArray;
        this.wrongAnswerConfig = this.jsonData.wrongAnswer;
        this.answersDuihuaConfig = this.jsonData.answersDuihua;

        Common.Type = 3;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.answersArray;

        this.curIndex = 0
  

        Game.ins.setGameTitle(this.titleArrayConfig[0]);
    }
    

    endGameView(delay = 4.5) {
        if(this.curIndex>=9){
            this.scheduleOnce(()=>{
                Game.ins.stopTime();
                this.renwuNode.active = false
                this.chaofanSpine.active = true
                this.chaofanSpine.getComponent(sp.Skeleton).setAnimation(0,'炒饭',true)
                Common5.playEffectCustom("baodanchaofan", "sound/炒菜");
                this.scheduleOnce(()=>{
                    Game.ins.showSuccess()
                },delay)  
            },2.5)
        }
     
    }
    // update (dt) {}
}