import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import { DateUtils } from "../../FrameWork/Util/DateUtils";
import Common5 from "../../Platform/th/Common5";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";
import InterfaceManager from "../Manager/InterfaceManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import GameBase from "../SCommon/GameBase";
import GetAward from "../SCommon/GetAward";


const { ccclass, property } = cc._decorator;


@ccclass
export default class ChaChang extends GameBase {
    @property([cc.SpriteFrame])
    chaColorSpFrame: cc.SpriteFrame[] = []

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

    localList = null
    _dt: number = 0
    shouyiNum = 20000000
    ad_refresh: boolean;
    onLoad() {
        super.onLoad()
    }

    onDestroy() {
        super.onDestroy()
    }

    start() {
        this.refrehView(false)
        this.refreshZhaoMuTime()
        this.showShouYiNode()
        EventMgr.onEvent_custom(ryw_Event.refreshZhaoMuTimes, () => {
            this.showShouYiNode()
            this.refreshZhaoMuTime()
        }, this)
        this.checkTask541()
    }
    update(dt) {
        this._dt += dt
        if (this._dt >= 1.0) {
            this._dt = 0
            this.refreshZhaoMuTime()
        }
    }
    refrehView(videoAD) {
        let maxLeng = 6
        let list = User.getChaChangList()
        console.log(list, 'ChaChangList++++++==')

        if (list.length == 0) {
            list.length = 0
            //首次进入 商人订单前4个默认出
            let freeIdArr = []

            let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
            let mainId = mainTaskInfo.Id
            // if (mainId == MainTaskIdEnum.MainTask_544 && BagManager.getBagGoodNums(1911) <= 0) {
            //     let tempArr = [1911]
            //     for (let i = 0; i < tempArr.length; i++) {
            //         // if(goodIdArr.indexOf)
            //         if (BagManager.getBagGoodNums(tempArr[i]) <= 0) {
            //             freeIdArr.push(tempArr[i])
            //         }
            //     }
            // }

            let goodArray = InterfaceManager.getMangheGoodsListByType('ChaChang', maxLeng, videoAD)

            // 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('ChaChang')) {
                if (this.ad_refresh) {//当前已经看过广告
                    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
                            this.ad_refresh = false
                            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++) {
                        if (BagManager.getBagGoodNums(taskCaiGouDan[i]) <= 0 && GMGoodIdArray.indexOf(taskCaiGouDan[i]) == -1) {
                            goodArray[0] = { goodId: taskCaiGouDan[i], goodNum: 1 }
                            break
                        }
                    }
                }
            }

            //打乱颜色顺序
            let colorList = []
            for (let i = 0; i < maxLeng; i++) {
                colorList[i] = i
            }
            let randomColorArr = Common5.shuffle(colorList)
            // let goodIdArr = []
            for (var i = 0; i < maxLeng; i++) {
                let obj = {
                    goodId: 0,
                    saveNum: 1,
                    colorIndex: randomColorArr[i]
                }
                let freeId = freeIdArr.pop()
                if (freeId) {
                    obj.goodId = freeId
                } else {
                    obj.goodId = goodArray[i].goodId
                }

                list[list.length] = obj
                // goodIdArr.push(goodArray[i].goodId)
            }
            list = Common5.shuffle(list)


            User.setChaChangList(list)
            console.log('茶场list==', list)


        }
        this.localList = list
        this.initView(list)
    }
    initView(list) {
        for (let i = 0; i < list.length; i++) {
            let childNode = this.node.getChildByName('mainNode').getChildByName('childNode_' + i)
            let goodPro = BagManager.getGoodsProperty(list[i].goodId)
            childNode.getChildByName('chaSpr').getComponent(cc.Sprite).spriteFrame = this.chaColorSpFrame[list[i].colorIndex]
            childNode.getChildByName('chaMingChen').getComponent(cc.Label).string = goodPro.goodName
        }
        this.freshContent(list)
    }
    freshContent(list) {
        for (let i = 0; i < list.length; i++) {
            let childNode = this.node.getChildByName('mainNode').getChildByName('childNode_' + i)
            if (list[i].saveNum > 0) {
                childNode.getChildByName('chaSpr').active = true
                childNode.getChildByName('paiZi').active = true
                childNode.getChildByName('chaMingChen').active = true
                childNode.getChildByName('采摘').active = true

                childNode.getChildByName('touchArea').active = true
            } else {
                childNode.getChildByName('chaSpr').active = false
                childNode.getChildByName('paiZi').active = false
                childNode.getChildByName('chaMingChen').active = false
                childNode.getChildByName('采摘').active = false

                childNode.getChildByName('touchArea').active = false
            }

        }
    }
    caiZhaiBtnClick(event, customData) {
        let list = User.getChaChangList()
        let callFunc = () => {
            list[customData].saveNum = 0
            User.setChaChangList(list)

            this.freshContent(list)

            // let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
            // let mainId = mainTaskInfo.Id
            // if (mainId == MainTaskIdEnum.MainTask_548) {
            //     let isHave = BagManager.isHaveAllGood([3715, 3716, 3717, 3718, 3719, 3720, 3721])
            //     if (isHave) {
            //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_548)
            //     }
            // } else if (mainId == MainTaskIdEnum.MainTask_554) {
            //     let isHave = BagManager.getBagGoodNums(1914)
            //     if (isHave) {
            //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_554)
            //     }
            // }

        }
        let childNode = this.node.getChildByName('mainNode').getChildByName('childNode_' + customData)
        childNode.getChildByName('采摘').active = false
        childNode.getChildByName('touchArea').active = false

        let arr = []
        arr.push({ goodId: list[customData].goodId, goodNum: 1 })

        // 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('ChaChang')) {
        //     if (this.ad_refresh ) {//当前已经看过广告
        //         let isfind = false
        //         for (let i = 0; i < GMGoodIdArray.length; i++) {
        //             if (BagManager.getBagGoodNums(GMGoodIdArray[i]) <= 0) {
        //                 arr[0] = { goodId: GMGoodIdArray[i], goodNum: 1 }
        //                 isfind = true
        //                 this.ad_refresh = false
        //                 break
        //             }
        //         }
        //         if (!isfind) {
        //             for (let i = 0; i < taskCaiGouDan.length; i++) {
        //                 if (BagManager.getBagGoodNums(taskCaiGouDan[i]) <= 0 && GMGoodIdArray.indexOf(taskCaiGouDan[i]) == -1) {
        //                     arr[0] = { goodId: taskCaiGouDan[i], goodNum: 1 }
        //                     break
        //                 }
        //             }
        //         }
        //     } else {
        //         for (let i = 0; i < taskCaiGouDan.length; i++) {
        //             if (BagManager.getBagGoodNums(taskCaiGouDan[i]) <= 0 && GMGoodIdArray.indexOf(taskCaiGouDan[i]) == -1) {
        //                 arr[0] = { goodId: taskCaiGouDan[i], goodNum: 1 }
        //                 break
        //             }
        //         }
        //     }
        // }

        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(arr, () => {
                callFunc()
            });
        })

    }
    refreshBtnClick() {
        //Common5.playEffect("sound/按键点击")
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-茶场重新进货`)
                    // Common5.ReportDY("inLevel", '茶场-AD-重新进货');
                    let list = User.getChaChangList()
                    list.length = 0
                    this.ad_refresh = true
                    this.refrehView(true)
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }
            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    zhaoMuBtnClick() {
        PrefabManage.loadPrefabByType(GameType.ChaChangZhaoMu)
    }
    refreshZhaoMuTime() {
        let localZhaoMuData = User.getChaChangZhaoMuData()
        let maxTime = localZhaoMuData.zhaoMuTime
        let zhaoMuBeiShu = localZhaoMuData.zhaoMuBeiShu
        // let maxUnlockIndex = localZhaoMuData[ZhaoMuGameType.SongHuaJiang].maxUnlockIndex

        if (maxTime > 0) {
            //按钮上人物显示第一个
            // let config = MeiNvZhaoMuManager.getConfigById(ZhaoMuGameType.XiaShuiDao,0)
            // this.zhaoMuNode.getChildByName('tipSpr').active = false
            this.zhaoMuNode.getChildByName('renSpine').active = true
            // this.zhaoMuNode.getChildByName('renSpine').getComponent(sp.Skeleton).setAnimation(0,'半身呼吸',true)
            // this.zhaoMuNode.getChildByName('renSpine').getComponent(sp.Skeleton).setSkin(config.skinIndex)
            this.zhaoMuNode.getChildByName('timeStr').getComponent(cc.Label).string = `${DateUtils.formatTime_custom(maxTime)}`
            this.zhaoMuNode.getChildByName('tipSpr').getChildByName('shouYiStr').getComponent(cc.Label).string = `收益x${zhaoMuBeiShu}`
        } else {
            // this.zhaoMuNode.getChildByName('tipSpr').active = true
            this.zhaoMuNode.getChildByName('renSpine').active = false
            this.zhaoMuNode.getChildByName('timeStr').getComponent(cc.Label).string = ``
            // this.zhaoMuNode.getChildByName('shouYiStr').getComponent(cc.Label).string = `秘书招聘`
            this.zhaoMuNode.getChildByName('tipSpr').getChildByName('shouYiStr').getComponent(cc.Label).string = `收益 x N`
        }

    }
    showShouYiNode() {
        let index = 1
        let localZhaoMuData = User.getChaChangZhaoMuData()
        let zhaoMuBeiShu = localZhaoMuData.zhaoMuBeiShu
        let money = this.shouyiNum * zhaoMuBeiShu
        this.node.getChildByName('shouYiNode').getChildByName('moneyStr').getComponent(cc.Label).string = `收益:${Common5.getNumberChangeHanzi(money, '1', 1)}/秒`
    }
    checkTask541() {
        // let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        // let mainId = mainTaskInfo.Id
        // if (mainId == MainTaskIdEnum.MainTask_541) {
        //     this.node.getChildByName('startBtnTask541').active = true
        //     this.node.getChildByName('MaskTask541').active = true
        // }
    }
    onBtnStartTask541() {
        this.node.getChildByName('MaskTask541').active = false
        this.node.getChildByName('startBtnTask541').active = false
        // PrefabManage.loadPrefabByType(GameType.XunZhaoChuFeng)
    }
}