// 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 GameReport from "../../../../FrameWork/Report/ZyZyReport";
import Common from "../../../../FrameWork/Util/Common";
import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";


import GeiHaoHaoYiGeJiaConfigs from "./GeiHaoHaoYiGeJiaConfigs";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GeiHaoHaoYiGeJia extends WordGameBaseComponent {

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

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

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

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

    @property(cc.Node)
    cehuangNv: cc.Node = null;
    @property(cc.Node)
    duihuaNode: cc.Node = null;

    

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    //当前进度
    curProgressIndex = 0
    //文本配置
    duihuaArrayConfig: any[] = [];
    unitListConfig: any = null;

    chuanxiaofuState = false
    nashuState = false
    //计时
    Timer = 0;
    chooseBResult: any = null;
    chooseAResult: any = null;
    curResult = null
    chooseTrue = 0

    chooseResult = []
    arrayChooseResult = ['chooseB','chooseB','chooseA','chooseA']
    isNextQuestion = false

    nanhaiDuihua = null
    bundle = null
    start () {
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle
        super.start();
        //Common5.playMusicCustom('WordGame','baoxiaoGame/TaXiangChuQuChuangChuang/sound/背景');
        DaDianScript.userEnterDaDian();

        this.initParameters();
        this.initComponent();
        this.chooseNode.active = false
        this.duihuaNode.active = false
        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveDirCheck, (data_) => {
            this.normalTouchCheckCallback(data_.targetNode);
        }, this);
    }

    normalTouchCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        switch (targetNode.name) {
            case '口袋遮罩':
                Game.ins.stopTime();
                this.node.getChildByName('声明2').active = true
                this.scheduleOnce(()=>{
                    let curLogNv = { str: '我同意', qiPaoPos: 2, effectUrl: 'sound/小孩语音/我同意'}
                    this.showDuiHua(curLogNv, ()=>{
                        //判断是不是一致的答案
                        this.cehuangNv.getChildByName('红灯').active = false
                        this.cehuangNv.getChildByName('绿灯').active = true
                        this.gangaNv.active = false
    
                        this.scheduleOnce(()=>{
                            this.endGameView(1);
                        },0.5)
                    })
                },2.0)
     

                break;
            default:
                break;
        }
    }

    //初始化参数
    initParameters() {
       
        this.duihuaArrayConfig = GeiHaoHaoYiGeJiaConfigs.getInstance().getGameConfig('questionList');
        
        console.log(this.duihuaArrayConfig )
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = GeiHaoHaoYiGeJiaConfigs.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = GeiHaoHaoYiGeJiaConfigs.getInstance().getGameConfig('answersArray');

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            this.endGameView(0);
        }, this);
        
        let curLog = { str: '我叫林乐青,是个单亲妈妈,这是我儿子浩浩', qiPaoPos: 1, effectUrl: 'sound/女语音/我叫林乐青,是个单亲妈妈,这是我儿子浩浩'}
        this.showDuiHua(curLog, ()=>{
            this.setChooseLab()
           
        })


    }

    //初始化组件
    initComponent() {
        Game.ins.setGameTitle(GeiHaoHaoYiGeJiaConfigs.getInstance().getGameConfig('titleArray'))
        // let lab = this.node.getChildByName("标题").getChildByName("lab");
        // lab.getComponent(cc.Label).string = GeiHaoHaoYiGeJiaConfigs.getInstance().getGameConfig('titleArray');

        // this.scheduleOnce(this.setTimeNodePos, 0);
    }

    //设置计时位置
    setTimeNodePos() {
        // let timeNode = this.node.getChildByName("timeNode")
        // let addtimeNode = this.node.getChildByName("tipsBtn").getChildByName("addtimeNode")
        // let timeNodePos = timeNode.getPosition()
        // let addtimeNodePos = addtimeNode.getPosition()
        // Game.ins.setTimePos(timeNodePos.x, timeNodePos.y);
        // Game.ins.setTimeScale(0.7)
        // Game.ins.setTimeBtnPos(addtimeNodePos.x, addtimeNodePos.y);
    }


    //对话
    showDuiHua(curLog, func?) {
        console.log("curLog==", curLog);

        if (curLog.effectUrl) {
            Common5.playEffectCustom(this.bundle, curLog.effectUrl);
        }

        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos

        
        console.log(string_.length)
        let delayTime_ = string_.length*0.25//curLog.delayTime || 3;

   
        if (qiPaoPos_ != -1) {
            let qiPaoList = this.node.getChildByName("qiPao");
            let qiPao = qiPaoList.getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .call(() => {
                    qiPao.active = false
                })
                .start();
        }


        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }

    endGameView(touchIndex) {
        this.node.getChildByName("mask").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            Game.ins.showFail();
        } else {
            Game.ins.showSuccess();
        }
    }

    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    touchStartNode(event) {
        let target = event.target;
        console.log('touchStartNode');
    }

    touchMoveNode(event) {

    }

    touchEndNode(event) {
        let target = event.target;
    }

    onClick(event) {
        let target = event.target;
        console.log("+++++++++++++++++++>" + target.name);
        switch (target.name) {
            case "button":
                break;

            default:
                break;
        }
    }

    setChooseLab(){
        let chooseALab = this.chooseNode.getChildByName('聊天框A').getChildByName('lab').getComponent(cc.Label)

        let chooseBLab = this.chooseNode.getChildByName('聊天框B').getChildByName('lab').getComponent(cc.Label)
        let data = this.duihuaArrayConfig[this.curProgressIndex]
        if(data){
            let questions =  data.questions

            chooseALab.string = questions.chooseA
            chooseBLab.string = questions.chooseB
    
            this.chooseNode.active = true
            this.chooseAResult = data.questionResult.chooseA
            this.chooseBResult =  data.questionResult.chooseB

        }else{
            //判断+++++
            this.panduanGameResult()
        }


        console.log(this.chooseAResult ,'this.chooseAResult +++===')
        console.log(this.chooseBResult ,'this.chooseBResult +++===')
    }
    
    bindData(){

    }


    cehuangyiRecovery(){
        this.cehuangNan.getChildByName('红灯').active = false
        this.cehuangNan.getChildByName('绿灯').active = false

        this.cehuangNv.getChildByName('红灯').active = false
        this.cehuangNv.getChildByName('绿灯').active = false
    }


    cehuangyiNanChange(isNan){
        this.cehuangNan.getChildByName('红灯').active = !isNan
        this.cehuangNan.getChildByName('绿灯').active = isNan

        Common5.playEffectCustom(this.bundle, 'sound/亮灯');
        if(isNan){
            this.chooseTrue++
        }

        if(this.chooseTrue == 2){
            //下一个问题
            this.nextQuestion()
        }

    }


    cehuangyiNvChange(isNv){
    
        this.cehuangNv.getChildByName('红灯').active = !isNv
        this.cehuangNv.getChildByName('绿灯').active = isNv
        Common5.playEffectCustom(this.bundle,'sound/亮灯');
        if(isNv){
            this.chooseTrue++
        }

        if(this.chooseTrue == 2){
            //下一个问题
            this.nextQuestion()
        }



    }
    nextQuestion(){
        if(this.isNextQuestion){
            return
        }
        this.isNextQuestion = true

        this.scheduleOnce(()=>{
            this.chooseTrue=0
            this.curProgressIndex++
            this.setChooseLab()
            this.cehuangyiRecovery()
       },1.0)
    }

    chooseBtnClickEvent(event, data){
        let key = data
        this.isNextQuestion = false

        let target = event.target
     
        let chooseBLab = target.getChildByName('lab').getComponent(cc.Label)
      
        GameReport.BtnsReport(chooseBLab, Common5.selectGameInfo.titleUrl);

        this.chooseResult.push(data)
        console.log(this.chooseResult,'this.chooseResult+++====')


        Common5.playEffectCustom(this.bundle,  'sound/问题语音/'+chooseBLab.string);
        this.chooseNode.active = false

        this.duihuaNode.active = true
        let duihuaLab = this.duihuaNode.getChildByName('lab').getComponent(cc.Label)
        duihuaLab.string = chooseBLab.string

        let delayTime = 2.0 //问题时间
        this.scheduleOnce(()=>{
            this.duihuaNode.active = false
            if(data == 'chooseA'){
                this.curResult = this.chooseAResult
            }else if(data == 'chooseB'){
                this.curResult = this.chooseBResult
            }
  
            //一起回答
    
            let nanArray =  this.curResult.nan.split(";")
            let nvArray =  this.curResult.nv.split(";")
            
            this.nanhaiDuihua = this.curResult.xiaohai
            
            let curLogNan = { str: nanArray[0], qiPaoPos: 0, delayTime:1.0, effectUrl: 'sound/男语音/'+nanArray[0]}
            this.showDuiHua(curLogNan, ()=>{
                if(nanArray[1] == 'true'){
                    this.cehuangyiNanChange(true)
                }else{
                    this.gangaNan.active = true

                    this.cehuangyiNanChange(false)
                 
                    if( nanArray[2]){
                        //下一个对话
                        this.scheduleOnce(()=>{
                            this.gangaNan.active = false
                            let curLogNan = { str: nanArray[2], qiPaoPos: 0, delayTime:1.0, effectUrl: 'sound/男语音/'+nanArray[2]}
                            this.showDuiHua(curLogNan, ()=>{
                            if(nanArray[3] == 'true'){
                                this.cehuangyiNanChange(true)
                            
                        
                            }
                        })
                        },2.0)
                    }else{

                    }
                }
            })
    
            let curLogNv = { str: nvArray[0], qiPaoPos: 1, delayTime:1.0, effectUrl: 'sound/女语音/'+nvArray[0]}
            this.showDuiHua(curLogNv, ()=>{
                if(nvArray[1] == 'true'){
                    this.cehuangyiNvChange(true)
                }else{
                    this.gangaNv.active = true
                    this.cehuangyiNvChange(false)
                    //下一个对话
                    if( nvArray[2]){
                        //下一个对话
                        this.scheduleOnce(()=>{
                            this.gangaNv.active = false
                            let curLogNv = { str: nvArray[2], qiPaoPos: 1, delayTime:1.0, effectUrl: 'sound/女语音/'+nvArray[2]}
                            this.showDuiHua(curLogNv, ()=>{
                                if(nvArray[3] == 'true'){
                                    this.cehuangyiNvChange(true)
                             
                                }
                            })
                        },2.0)
                    }else{
                        
                    }
                }
            })

            if(this.nanhaiDuihua){
                this.scheduleOnce(()=>{
                    let curLogNv = { str: '我不同意', qiPaoPos: 2, delayTime:1.0, effectUrl: 'sound/小孩语音/我不同意'}
                    this.showDuiHua(curLogNv, ()=>{
                        //判断是不是一致的答案
                        this.panduanGameResult()
                    })
                },1.5)
            }
      
        },delayTime)
        
        
   
        
    }



    panduanGameResult(){
        if(this.chooseResult[0] == this.arrayChooseResult[0] &&
            this.chooseResult[1] == this.arrayChooseResult[1] &&
            this.chooseResult[2] == this.arrayChooseResult[2] &&
            this.chooseResult[3] == this.arrayChooseResult[3] ){
            
            //触发剧情成功
            this.node.getChildByName('touchHide').active = false

        }else{
            //
            Common5.playEffectCustom(this.bundle, 'sound/问题语音/经过询问,你们并不适合结婚');
            Game.ins.stopTime();
            this.scheduleOnce(()=>{
                this.node.getChildByName('failNode').active = true
                this.scheduleOnce(()=>{
                    this.endGameView(0);
                },1.0)
            },3.0)
          
        }
    }
    dengShow(){

    }

    // update (dt) {}
}