import GameReport from "../../FrameWork/Report/ZyZyReport";
import User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import TaskManager from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";
import InterfaceManager from "../Manager/InterfaceManager";
import ShipuManager from "../Manager/ShipuManager";
import UserManager from "../Manager/UserManager";

import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GameBase from "../SCommon/GameBase";
import GetAward from "../SCommon/GetAward";

import DiaoYuConfigManage from "./DiaoYuConfigManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DiaoYu extends GameBase {
    @property(sp.Skeleton)
    diaoYuSp: sp.Skeleton = null;

    @property({ type: cc.Node, tooltip: '开始钓鱼' })
    diaoyuKaishiNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: '路亚' })
    diaoyuLuyaNode: cc.Node = null;


    wulianIndex = 0
    currentYugou = '鱼饵'
    _dt: number = 0
    private dongxi = {
        jingzhi: 'jingzhi',
        chugan: 'chugan',
        chuganhoudaiji: 'chuganhoudaiji',
        lagan: 'lagan',
        laganzhuan1: 'laganzhuan1',
        laganzhuan2: 'laganzhuan2',
        laganzhuan3: 'laganzhuan3',
        qigan: 'qigan'
    }
    onDestroy() {
        super.onDestroy()
    }

    onLoad() {
        super.onLoad()


    }

    start() {
        Common5.playMusic("CommonRes/sound/稻田钓鱼背景");

        this.diaoyuBtnInit();



        // let mainTaskInfo:any = TaskManager.getCurUnLockMainTaskInfo()
        // let mainId = mainTaskInfo.Id

        // if(mainId == MainTaskIdEnum.MainTask_506 ){
        //     ProbabilityManager.setCanChangeDiaoyuIndex(3)
        // }else{
        //     ProbabilityManager.setCanChangeDiaoyuIndex(2)
        // }

    }



    zhulanOldPos = null;
    zhulan_cb() {

    }
    handleZhulan() {
        //Common5.playEffect("sound/按键点击")



    }


    showResults(result) {
        let num = result.length;

        // console.log(arr, 'arr++++++++====')
        // let isHavePro = false
        // if (result.length > 1) {

        //     if (BagManager.getBagGoodNums(1724) <= 0) {
        //         for (let i = 0; i < result.length; i++) {
        //             let dataArray = result[i]
        //             if (dataArray.goodId >= 1701 && dataArray.goodId <= 1750) {

        //             } else {
        //                 dataArray.goodId = 1724
        //                 isHavePro = true
        //                 break
        //             }
        //         }
        //     }
        // }


        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(result, () => {
                //完成任务525
                // if(isHavePro){
                //     let jiesuoId = 5
                //     ShipuManager.shipuUnLockById(jiesuoId)
                // }
                ShipuManager.setShipuUnLock()
            }, true);
        }, false)

    }


    setYugouStatus(name) {
        // let data = DiaoYuManage.getYuerByName(name);
        // let data1 = DiaoYuConfigManage.getDiaoyuYuerConfigByName(name);
        // let yugouNode = this.yugouNode1
        // let needTimes = 0;


        // if(data.unlock){
        //     yugouNode.getChildByName('sp').active = false;
        //     yugouNode.getChildByName('price').active = true;
        //     yugouNode.getChildByName('times').active = false;
        //     yugouNode.getChildByName('price').getComponent(cc.Label).string = Common5.getNumberChangeHanzi(data1.price)  + ''
        //     if(data.freeTimes > 0){
        //         yugouNode.getChildByName('times').active = true; 
        //         yugouNode.getChildByName('times').getComponent(cc.Label).string = '免费次数' + data.freeTimes;
        //     }
        // }else{
        //     yugouNode.getChildByName('sp').active = true;
        //     yugouNode.getChildByName('price').active = false;
        //     if(needTimes > 0){
        //         yugouNode.getChildByName('times').active = true;
        //         yugouNode.getChildByName('times').getComponent(cc.Label).string = '('+data.spTimes + '/' + needTimes+')'
        //     }else{ 
        //         yugouNode.getChildByName('times').active = false;
        //     } 
        // }
    }

    btns_init(setSKin = true) {



    }
    setYugouSkin(name) {
        let data = DiaoYuConfigManage.getDiaoyuYuerConfigByName(name);
        this.diaoYuSp.setSkin(data.spinSkin);
        this.diaoYuSp.setAnimation(1, this.dongxi.jingzhi, true);
    }
    handleYugouClick() {
        //Common5.playEffect("sound/按键点击")
        if (this.isDiaoyu) {
            return;
        }

        this.reportKey(() => {
            GameReport.BtnsReport('鱼钩')
        })
        this.currentYugou = '鱼钩';
        this.setYugouSkin('鱼钩');

        this.btns_init();
    }



    // 关闭提示框
    handleCloseTips() {
        //Common5.playEffect("sound/按键点击")

    }


    diaoyuBtnInit() {
        this.diaoyuKaishiNode.active = true;
        this.diaoyuLuyaNode.active = true;


    }
    zhunbeiBtnSet() {
        this.diaoyuKaishiNode.active = false;
        this.diaoyuLuyaNode.active = false;

    }
    taiganBtnSet() {
        this.diaoyuKaishiNode.active = false;
        this.diaoyuLuyaNode.active = false;
        //钓鱼
        this.handleTaigan()


    }

    private zhunbeiBeginTime = -1
    private isDiaoyu = false;
    private isLuya = false;

    taigan_schedule_cb() {
        this.isDiaoyu = false;
        this.isLuya = false;
        this.diaoyuBtnInit();
        this.isDiaoyu = false;
        this.btns_init();
        this.diaoYuSp.setAnimation(1, this.dongxi.jingzhi, true)
        this.isZhunbeiTaigan = false;
        cc.audioEngine.stopAllEffects();
        this.setYugouSkin(this.currentYugou);
    }
    begin_diaoyu() {

        this.zhunbeiBeginTime = -1;
        // 甩干
        this.diaoYuSp.setAnimation(1, this.dongxi.chugan, false);
        this.diaoYuSp.timeScale = 1
        // 等待

        let xiaofuTime = 2.0

        let rushuiTime = 1
        cc.tween(this.diaoYuSp.node)
            .delay(1.3)
            .call(() => {
                this.diaoYuSp.timeScale = 0.5
                // 进入甩干待机状态
                this.diaoYuSp.setAnimation(1, this.dongxi.chuganhoudaiji, true);
                Common5.playRemoteAudioEffect('sound/diaoyu/钓鱼鱼饵入水')
            })
            .delay(rushuiTime)
            .call(() => {
                Common5.playRemoteAudioEffect('sound/diaoyu/钓鱼浮漂下沉咬钩')

                this.diaoYuSp.setAnimation(1, this.dongxi.lagan, false);
                this.zhunbeiBeginTime = Date.now();
            })
            .delay(1.0)
            .call(() => {
                this.isZhunbeiTaigan = true;
                this.scheduleOnce(this.taiganBtnSet, 2);
                //this.endPointNode.active = true;
                let randomx = Common5.getRandomNum(0, 3)
                if (randomx == 0) {
                    this.diaoYuSp.setAnimation(1, this.dongxi.laganzhuan1, false);
                } else if (randomx == 1) {
                    this.diaoYuSp.setAnimation(1, this.dongxi.laganzhuan2, false);
                } else if (randomx == 2) {
                    this.diaoYuSp.setAnimation(1, this.dongxi.laganzhuan3, false);
                }


            })
            .start()
    }
    private isZhunbeiTaigan = false;


    private isTaigan = false;
    handleTaigan() {
        //Common5.playEffect("sound/按键点击")
        this.unschedule(this.taigan_schedule_cb);
        if (this.isTaigan) {
            //this.endPointNode.active = false;
            return;
        }

        this.isZhunbeiTaigan = false;
        cc.audioEngine.stopAllEffects();
        this.isTaigan = true;

        if (1 == 1) {
            this.diaoYuSp.setAnimation(1, this.dongxi.qigan, false)

            let num = 1;
            let result = InterfaceManager.getMangheGoodsListByType('Diaoyu', num, false)//DiaoYuConfigManage.getGoodByProbability(this.currentYugou, num,false);
            if (this.isLuya) {
                num = 5;
                // this.currentYugou = '路亚'
                result = InterfaceManager.getMangheGoodsListByType('Diaoyu', num, true)//DiaoYuConfigManage.getGoodByProbability('路亚', num,true);
            }

            let curNum = Number(cc.sys.localStorage.getItem("钓鱼单点次数") ?? 0)
            curNum++
            cc.sys.localStorage.setItem("钓鱼单点次数", `${curNum}`)
            let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            let configData = TaskManager.getTaskConfigById(mainId)
            let GMGoodIdArray = configData.GMGoodIdArray
            let taskCaiGouDan = configData.taskCaiGouDan
            if (taskCaiGouDan && taskCaiGouDan.length > 0 && configData.GMGameType && configData.GMGameType.includes('DiaoYu')) {
                if (this.isLuya) {//当前已经看过广告
                    for (let i = 0; i < GMGoodIdArray.length; i++) {
                        if (BagManager.getBagGoodNums(GMGoodIdArray[i]) <= 0) {
                            result[0] = { goodId: GMGoodIdArray[i], goodNum: 1 }
                            break
                        }
                    }
                } else {
                    for (let i = 0; i < taskCaiGouDan.length; i++) {
                        let ifor = (i == 0 && curNum >= 1) || (i == 1 && curNum >= 10) || (i == 2 && curNum >= 10)
                        let indexof = GMGoodIdArray.indexOf(taskCaiGouDan[i]) == -1
                        if (ifor) {
                            indexof = true
                        }
                        if (BagManager.getBagGoodNums(taskCaiGouDan[i]) <= 0 && indexof) {
                            result[0] = { goodId: taskCaiGouDan[i], goodNum: 1 }
                            cc.sys.localStorage.setItem("钓鱼单点次数", `${0}`)
                            break
                        }
                    }
                }
            }

            cc.tween(this.diaoYuSp.node)
                .delay(2.0)
                .call(() => {
                    this.showResults(result);
                    this.diaoyuBtnInit();
                    this.isDiaoyu = false;
                    this.isTaigan = false;
                    this.btns_init();
                    //this.endPointNode.active = false;
                    this.diaoYuSp.setAnimation(1, this.dongxi.jingzhi, true);
                    console.log(this.currentYugou, 'this.currentYugou+++++++11+====')
                    this.setYugouSkin(this.currentYugou);
                })
                .start();
        }

    }
    // 开始钓鱼
    handleDiaoyu() {
        //Common5.playEffect("sound/按键点击")
        if (this.isDiaoyu) {
            return;
        }
        if (User.getMoney() >= 1000000000) {
            UserManager.subMoney(1000000000)
        } else {
            PrefabManage.showTextTips("金额不足")
            return
        }
        this.isLuya = false;
        this.isDiaoyu = true;
        this.zhunbeiBeginTime = -1;
        this.begin_diaoyu();
    }

    handleDiaoyuLuya() {
        //Common5.playEffect("sound/按键点击")
        if (this.isDiaoyu) {
            return;
        }

        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-钓鱼五连竿`)
                    // Common5.ReportDY("inLevel", "松花江-AD-五连竿");
                    this.isLuya = true;
                    this.isDiaoyu = true;
                    this.diaoYuSp.setSkin('luyayugou')
                    this.begin_diaoyu();

                }
                else {

                    Common5.showTips_custom("广告未观看完");
                }

            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }



}