import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import GameBaseScript from "../GameRoomRes/GameBaseScript";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import TaskResult from "../MyRoom/TaskResult";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";



let gameConfig = {
    duihuaArray: [
        {
            str: "你帮我选选吧,选左还是右",
            qiPaoPos: 0,
            delayTime: 3.4,
            effectUrl: "GameStoryRes/sound/GameStory3/你帮我选选吧,选左还是右"
        },
        {
            str: "你这什么眼光啊",
            qiPaoPos: 0,
            delayTime: 1.4,
            effectUrl: "GameStoryRes/sound/GameStory3/你这什么眼光啊"
        },
        {
            str: "很漂亮,是吧",
            qiPaoPos: 0,
            delayTime: 1.8,
            effectUrl: "GameStoryRes/sound/GameStory3/很漂亮,是吧"
        },
        {
            str: "这个我很喜欢",
            qiPaoPos: 0,
            delayTime: 1.1,
            effectUrl: "GameStoryRes/sound/GameStory3/这个我很喜欢"
        },
        {
            str: "别看了,赶紧去经营湘菜馆!",
            qiPaoPos: 0,
            delayTime: 3.1,
            effectUrl: "GameStoryRes/sound/GameStory3/别看了,赶紧去经营湘菜馆!"
        }
    ],
    chooseArray: [
        {
            btnArray: [
                "修女cos",
                "可爱风"
            ]
        },
        {
            btnArray: [
                "睡衣穿搭",
                "真丝吊带"
            ]
        },
        {
            btnArray: [
                "不穿",
                "穿"
            ]
        }
    ]
}
const { ccclass, property } = cc._decorator;

@ccclass
export default class GameStory3 extends cc.Component {
    // @property(cc.Node)
    // chooseLayer: cc.Node = null;

    @property([cc.Node])
    role: cc.Node[] = [];

    @property([cc.Node])
    costumeList: cc.Node[] = [];

    // @property([cc.Node])
    // operatedNodeList: cc.Node[] = [];

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

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


    //当前层级下标
    curLayerIndex = 0;
    //当前进度
    curSchedule: number = 0;
    chooseIndex: number = -1;

    //文本配置
    // titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chooseArrayConfig: any = null;

    start() {
        GameBaseScript.preLoadRemoteAudio(gameConfig);
        PrefabManage.showTextTips('为投资人换上合适的衣服');
        this.initParameters();
        this.initComponent();

    }
    restart() {
        this.initParameters();
        this.initComponent();
    }
    //初始化参数
    initParameters() {
        this.curLayerIndex = 0;
        this.curSchedule = 0;
        this.chooseIndex = -1;

        // this.titleArrayConfig = this.jsonData.titleArray//ZhouWangDeKuaiLeConfig.getInstance().getGameConfig('titleArray') as string[];
        this.duihuaArrayConfig = gameConfig.duihuaArray//this.jsonData.duihuaArray//ZhouWangDeKuaiLeConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.chooseArrayConfig = gameConfig.chooseArray//this.jsonData.chooseArray//ZhouWangDeKuaiLeConfig.getInstance().getGameConfig('chooseArray');

    }

    //初始化组件
    initComponent() {

        for (const iterator of this.role) {
            iterator.active = false;
        }
        for (const iterator of this.costumeList) {
            iterator.active = false;
        }

        this.playAnimation2(this.role[0], '待机', true, 'default');
        // this.showDuiHua(this.duihuaArrayConfig[0], () => {
        //     this.showDuiHua(this.duihuaArrayConfig[1], () => {
        this.refreshSchedule();
        //     });
        // });

    }



    //刷新进度
    refreshSchedule() {
        switch (this.curSchedule) {
            case 0:
                this.playAnimation2(this.role[0], '待机', true);
                this.showDuiHua(this.duihuaArrayConfig[0], () => {
                    this.costumeList[this.curSchedule].active = true;
                    this.showChooseLayer(0);
                });
                break;
            case 1:
                this.playAnimation2(this.role[0], '待机', true);
                this.costumeList[this.curSchedule].active = true;
                this.showChooseLayer(1);
                break;
            case 2:
                this.playAnimation2(this.role[0], '待机', true);
                this.costumeList[this.curSchedule].active = true;
                this.showChooseLayer(2);
                break;
            default:
                break;
        }
    }
    playAnimation2(
        spineNode: cc.Node,
        animName: string,
        loop: boolean = false,
        skinName: string = null,
        completeCallback: (spineNode: cc.Node) => void = null
    ) {
        spineNode.active = true;
        if (skinName && skinName != '') {
            spineNode.getComponent(sp.Skeleton).setSkin(skinName);
        }
        if (completeCallback) {
            spineNode.getComponent(sp.Skeleton).setCompleteListener(() => {
                completeCallback(spineNode);
            });
        }
        spineNode.getComponent(sp.Skeleton).setAnimation(0, animName, loop);
    }
    // //触摸回调
    // normalTouchCallback(targetNode) {
    //     console.log("+++++++++++++++++++++>" + targetNode.name);
    //     switch (targetNode.name) {
    //         case '脱衣服':
    //             this.chooseLayer.active = false;
    //             this.role[1].active = false;
    //             this.playAnimation2(this.role[0], '害羞', true, 'default');
    //             this.showDuiHua(this.duihuaArrayConfig[10], () => {
    //                 this.endGameView(1);
    //             });
    //             break;

    //         default:
    //             break;
    //     }
    // }

    //选择回调
    chooseResultCallback(touchIndex: number) {
        this.node.getChildByName("mask").active = false;
        this.btnLeft.active = false
        this.btnRight.active = false

        switch (this.chooseIndex) {
            case 0:
                this.costumeList[this.curSchedule].active = false;
                if (touchIndex == 0) {
                    // this.playAnimation2(this.role[0], '制服', true, '');
                    // this.showDuiHua(this.duihuaArrayConfig[1], () => {
                    //     this.endGameView(0);
                    // });
                    let tab = {
                        onClose: (finish) => {
                            if (finish) {
                                Common5.ReportDY("inLevel", "换装-AD-修女cos");
                                this.playAnimation2(this.role[0], '制服', true, '');
                                this.showDuiHua(this.duihuaArrayConfig[2], () => {
                                    this.curSchedule++;
                                    this.refreshSchedule();
                                });
                            } else {
                                Common5.showTips_custom("广告未观看完");
                            }
                        }, onFailed: () => {
                        }
                    }
                    AppPlatform.playVideo_custom(tab)
                } else {
                    this.playAnimation2(this.role[0], '牛仔裙', true, '');
                    this.showDuiHua(this.duihuaArrayConfig[2], () => {
                        this.curSchedule++;
                        this.refreshSchedule();
                    });
                }
                break;
            case 1:
                this.costumeList[this.curSchedule].active = false;
                if (touchIndex == 0) {
                    this.playAnimation2(this.role[0], '睡衣', true, '');
                    // this.showDuiHua(this.duihuaArrayConfig[1], () => {
                    //     this.endGameView(0);
                    // });
                    this.showDuiHua(this.duihuaArrayConfig[3], () => {
                        this.curSchedule++;
                        this.refreshSchedule();
                    });
                } else {
                    let tab = {
                        onClose: (finish) => {
                            if (finish) {
                                Common5.ReportDY("inLevel", "换装-AD-真丝吊带");
                                this.playAnimation2(this.role[0], '礼服', true, '');
                                this.showDuiHua(this.duihuaArrayConfig[3], () => {
                                    this.curSchedule++;
                                    this.refreshSchedule();
                                });
                            } else {
                                Common5.showTips_custom("广告未观看完");
                            }
                        }, onFailed: () => {
                        }
                    }
                    AppPlatform.playVideo_custom(tab)
                }
                break;
            case 2:
                this.costumeList[this.curSchedule].active = false;
                if (touchIndex == 0) {
                    // this.role[0].active = false;
                    // this.playAnimation2(this.role[0], '粉裙', true);
                    // this.showDuiHua(this.duihuaArrayConfig[7], () => {
                    this.endGameView(0);
                    // });
                } else {
                    this.role[0].active = false;
                    this.role[1].active = true;

                    this.showDuiHua(this.duihuaArrayConfig[4], () => {
                        this.endGameView(1);
                    });
                }
                break;
            default:
                break;
        }
    }

    //显示选项
    showChooseLayer(chooseIndex: number) {
        this.btnLeft.active = true
        this.btnRight.active = true
        this.btnLeft.getChildByName('str').getComponent(cc.Label).string = this.chooseArrayConfig[chooseIndex].btnArray[0]
        this.btnRight.getChildByName('str').getComponent(cc.Label).string = this.chooseArrayConfig[chooseIndex].btnArray[1]
        // this.chooseLayer.active = true;
        this.chooseIndex = chooseIndex;
        // Common.GameSubChooseArray = this.chooseArrayConfig[chooseIndex];
        // let script_: ChooseBtnLayer = this.chooseLayer.getComponent('ChooseBtnLayer');
        // script_.setChooseBtnView();

        if (this.chooseIndex == 0) {
            this.btnLeft.getChildByName('蓝色按钮视频').active = true
        } else {
            this.btnLeft.getChildByName('蓝色按钮视频').active = false
        }

        if (this.chooseIndex == 1) {
            this.btnRight.getChildByName('蓝色按钮视频').active = true
        } else {
            this.btnRight.getChildByName('蓝色按钮视频').active = false
        }
    }

    onBtnLeftClick() {
        this.chooseResultCallback(0)
    }
    onBtnRightClick() {
        this.chooseResultCallback(1)
    }
    //显示对话
    showDuiHua(curLog, func?) {
        console.log("curLog==", curLog);

        // if (curLog.effectUrl) {
        //     Common5.playEffectCustom("zhouwangdekuaile", curLog.effectUrl);
        // }

        let string_ = curLog.str;
        let qiPaoPos_ = curLog.qiPaoPos;
        let delayTime_ = curLog.delayTime || 3;

        let node = this.node.getChildByName('layerNode');
        if (qiPaoPos_ != -1) {
            if (curLog.effectUrl && curLog.effectUrl.length > 0) {
                Common5.playRemoteAudioEffect(curLog.effectUrl);
            }
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            let effect = node.getChildByName("声效").getChildByName("声效" + qiPaoPos_);
            qiPao.stopAllActions();
            effect.active = true;
            // qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            this.showDialogStr(string_, qiPao.getChildByName("str"))
            cc.tween(qiPao)
                .set({ active: true, scale: 0 })
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .call(() => {
                    qiPao.active = false;
                    effect.active = false;
                })
                .start();
        }

        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }
    showDialogStr(str, dialogStr, finishFunc?) {
        let curStr = ""
        let curIndex = 0
        let callFunc = () => {
            curStr += str[curIndex++]
            dialogStr.getComponent(cc.Label).string = curStr
            if (curIndex >= str.length) {
                finishFunc && finishFunc()
            }
        }
        this.schedule(callFunc, 0.07, str.length - 1)
    }
    //关闭对话
    closeDuiHua(qiPaoPos_) {
        if (qiPaoPos_ != -1) {
            let node = this.node.getChildByName("layerNode");
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions();
            qiPao.active = false;
        } else {
            let logPrefab = this.node.getChildByName('logPrefab');
            let duihua = logPrefab.getChildByName('duihua');
            duihua.stopAllActions();
            duihua.active = false;
        }
    }

    endGameView(touchIndex) {
        let finishTask306 = () => {

            TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_306)
            TaskManager.setCurUnLockMainTaskInfo(MainTaskIdEnum.MainTask_3062)
            // ZhongCanTingManager.checkTask()

            this.node.removeFromParent()
            this.node.destroy()
            // Common5.releaseBundleAssets('GameStory3')
        }
        // this.node.getChildByName("mask").active = true;
        // Game.ins.stopTime();
        if (touchIndex == 0) {
            this.scheduleOnce(() => {
                let continueBtnFunc = () => {
                    console.log('换衣重玩')
                    this.restart()
                }
                let adBtnFunc = () => {
                    console.log('换衣完成任务')
                    // Common5.ReportDY("inLevel", '换衣服关卡-AD-老婆换最美衣服');
                    finishTask306()
                }
                PrefabManage.loadPrefabByType(GameType.TaskResult, null, (prefabNode) => {
                    prefabNode.getComponent(TaskResult).initView4(2, true, continueBtnFunc, adBtnFunc)
                })
            }, 0.5);
        } else {
            this.scheduleOnce(() => {
                let successFunc = () => {

                    finishTask306()
                }
                PrefabManage.loadPrefabByType(GameType.TaskResult, null, (prefabNode) => {
                    prefabNode.getComponent(TaskResult).initView1(3, true, successFunc)
                })
            }, 0.5);
        }
    }

    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
        let logPrefab = this.node.getChildByName('logPrefab')
        logPrefab.getChildByName('duihua').active = false;
    }

    onClick(event) {
        let target = event.target as cc.Node;
        console.log("+++++++++++++++++++++>" + target.name);
        // Common5.playEffect("点击音效");
        switch (target.name) {
            case "BtnAddTimes":
            default:
                break;
        }
    }

    //update(dt) { }
}