import User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";
import InterfaceManager from "../Manager/InterfaceManager";
import UserManager from "../Manager/UserManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GetAward from "../SCommon/GetAward";

const { ccclass, property } = cc._decorator;

@ccclass
export default class XiuXianNongZhuang extends cc.Component {


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    isAction: boolean = false

    start() {

    }

    onClickGeYiGe() {
        if (this.isAction) {
            return
        }
        if (User.getMoney() >= 10000000000000) {
            UserManager.subMoney(10000000000000)
        } else {
            PrefabManage.showTextTips("金额不足")
            return
        }
        this.showAni(1, false)
    }
    onClickGeGeShuang() {
        if (this.isAction) {
            return
        }
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-休闲农庄割个爽`)
                    // Common5.ReportDY("inLevel", '割个爽-AD-休闲农庄');
                    this.showAni(5, true)
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }

            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    showAni(num, isAD?) {
        Common5.playEffectCustom("XiuXianNongZhuang", 'sound/割草');

        this.isAction = true
        let cao1 = this.node.getChildByName('cao1')
        let cao2 = this.node.getChildByName('cao2')
        let hua1 = this.node.getChildByName('hua1')
        let hua2 = this.node.getChildByName('hua2')
        cao1.getComponent(sp.Skeleton).setAnimation(0, '草', true)
        cao2.getComponent(sp.Skeleton).setAnimation(0, '草', true)
        hua1.getComponent(sp.Skeleton).setAnimation(0, '花丛', true)
        hua2.getComponent(sp.Skeleton).setAnimation(0, '花丛', true)

        this.scheduleOnce(() => {
            cao1.getComponent(sp.Skeleton).setAnimation(0, '草待机', true)
            cao2.getComponent(sp.Skeleton).setAnimation(0, '草待机', true)
            hua1.getComponent(sp.Skeleton).setAnimation(0, '花丛待机', true)
            hua2.getComponent(sp.Skeleton).setAnimation(0, '花丛待机', true)
            this.showAward(num, isAD)
        }, 2)
    }
    showAward(awardNum, isAD?) {
        let goodArray = InterfaceManager.getMangheGoodsListByType('XiuXianNongZhuang', awardNum, isAD)

        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('XiuXianNongZhuang')) {
            if (isAD) {//当前已经看过广告
                let isfind = false
                for (let i = 0; i < GMGoodIdArray.length; i++) {
                    if (BagManager.getBagGoodNums(GMGoodIdArray[i]) <= 0) {
                        goodArray[0] = { goodId: GMGoodIdArray[i], goodNum: 1 }
                        isfind = true
                        break
                    }
                }
                if (!isfind) {
                    for (let i = 0; i < taskCaiGouDan.length; i++) {
                        if (BagManager.getBagGoodNums(taskCaiGouDan[i]) <= 0 && GMGoodIdArray.indexOf(taskCaiGouDan[i]) == -1) {
                            goodArray[0] = { goodId: taskCaiGouDan[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) || (i == 3 && curNum >= 10) || (i == 4 && curNum >= 10)
                    let indexof = GMGoodIdArray.indexOf(taskCaiGouDan[i]) == -1
                    if (ifor) {
                        indexof = true
                    }
                    if (BagManager.getBagGoodNums(taskCaiGouDan[i]) <= 0 && indexof) {
                        goodArray[0] = { goodId: taskCaiGouDan[i], goodNum: 1 }
                        cc.sys.localStorage.setItem("割草单点次数", `${0}`)
                        break
                    }
                }
            }
        }

        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(goodArray, () => {
                this.isAction = false
                this.checkTask()

            });
        })
    }

    checkTask() {
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        // if (BagManager.isHaveAllGood([1724, 1725, 1726]) && mainId == MainTaskIdEnum.MainTask_638) {
        //     TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_638)
        // } else if (mainId == MainTaskIdEnum.MainTask_611) {
        //     TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_611)
        // }
    }
    // update (dt) {}
}