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 ChatManager from "../JuQingChat/ChatManager";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import { AutoShouYiConfig } from "../MainScene/MainScene";
import BagManager from "../Manager/BagManager";
import InterfaceManager from "../Manager/InterfaceManager";
import MeiNvZhaoMu from "../MeiNvZhaoMu/MeiNvZhaoMu";
import MeiNvZhaoMuManager, { ZhaoMuGameType } from "../MeiNvZhaoMu/MeiNvZhaoMuManager";
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 BaiCaoJi extends GameBase {
    @property(cc.Node)
    allFlowerNode: cc.Node = null;

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

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

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

    // localList = null
    _dt: number = 0
    isVideo: any;
    onLoad() {
        super.onLoad()
    }

    onDestroy() {
        super.onDestroy()
    }

    start() {
        this.refrehHuaView(false)
        this.refrehShuView(false)
        this.refreshBtnStatus()
        this.refreshZhaoMuTime()
        this.showShouYiNode()
        EventMgr.onEvent_custom(ryw_Event.refreshZhaoMuTimes, () => {
            this.showShouYiNode()
            this.refreshZhaoMuTime()
        }, this)
    }

    showShouYiNode() {
        let index = ZhaoMuGameType.QiZhenYuan
        let localZhaoMuData = MeiNvZhaoMuManager.getMeiNvZhaoMuData()
        let zhaoMuBeiShu = localZhaoMuData[index].zhaoMuBeiShu

        let autoIndex = 14
        let money = AutoShouYiConfig[autoIndex].money * zhaoMuBeiShu
        this.node.getChildByName('shouYiNode').getChildByName('moneyStr').getComponent(cc.Label).string = `收益:${Common5.getNumberChangeHanzi(money, '1', 1)}/秒`
    }

    update(dt) {
        this._dt += dt
        if (this._dt >= 1.0) {
            this._dt = 0
            this.refreshZhaoMuTime()
        }
    }

    refrehHuaView(videoAD) {
        let maxLeng = 6
        let list = User.getBaiCaoJiList()
        console.log(list, 'BaiCaoJiList++++++==')

        if (list.length == 0) {
            list.length = 0
            //首次进入 商人订单前4个默认出
            let freeIdArr = []
            let daoYeList = InterfaceManager.getDaoyeList()
            if (daoYeList.length > 0 && daoYeList[0].gameType == '御花园') {
                // let tempArr = [1789,1790,1791,1792]
                // 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('BaiCaoJi', 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('佰草集')) {
                if (this.isVideo) {//当前已经看过广告
                    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.isVideo = 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
                }
                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.setBaiCaoJiList(list)
            console.log('佰草集list==', list)


        }
        // this.localList = list
        this.initHuaView(list)
    }
    refrehShuView(videoAD) {
        let maxLeng = 3
        let list = User.getGuShuList()
        console.log(list, 'gushuList++++++==')

        if (list.length == 0) {
            list.length = 0
            //首次进入 商人订单前3个默认出
            let freeIdArr = []
            let daoYeList = InterfaceManager.getDaoyeList()
            if (daoYeList.length > 0 && daoYeList[0].gameType == '御花园2') {
                // let tempArr = [2751,2752,2753]
                // 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('GuShu', maxLeng, videoAD)

            for (var i = 0; i < maxLeng; i++) {
                let obj = {
                    goodId: 0,
                    saveNum: 1
                }
                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.setGuShuList(list)
            console.log('古树list==', list)


        }
        // this.localList = list
        this.initShuView(list)
    }
    initHuaView(list) {
        let childLength = this.allFlowerNode.childrenCount
        for (let i = 0; i < list.length; i++) {
            let goodPro = BagManager.getGoodsProperty(list[i].goodId)
            for (let j = 0; j < childLength; j++) {
                if (this.allFlowerNode.children[j].name == ('f_' + i)) {
                    let childNode = this.allFlowerNode.children[j]

                    if (list[i].goodId >= 3722 && list[i].goodId <= 3729) {
                        Common5.addUrlSprite_custom('BaiCaoJi/任务花1/' + goodPro.goodName, childNode.getComponent(cc.Sprite))
                    } else {
                        Common5.addUrlSprite_custom('BaiCaoJi/御花园花/' + goodPro.goodName, childNode.getComponent(cc.Sprite))
                    }

                }
            }

        }
        this.freshHuaContent(list)
    }
    initShuView(list) {
        let childLength = this.allTreeNode.childrenCount
        for (let j = 0; j < childLength; j++) {
            let childNode = this.allTreeNode.children[j]
            let goodPro = BagManager.getGoodsProperty(list[j].goodId)

            Common5.addUrlSprite_custom('BaiCaoJi/御花园树/' + goodPro.goodName, childNode.getComponent(cc.Sprite))
        }
        this.freshShuContent(list)
    }
    freshHuaContent(list) {
        let childLength = this.allFlowerNode.childrenCount
        for (let i = 0; i < list.length; i++) {
            for (let j = 0; j < childLength; j++) {
                if (this.allFlowerNode.children[j].name == ('f_' + i)) {
                    let childNode = this.allFlowerNode.children[j]
                    if (list[i].saveNum > 0) {
                        childNode.active = true
                    } else {
                        childNode.active = false
                    }
                }
            }

        }
    }
    freshShuContent(list) {
        for (let i = 0; i < list.length; i++) {
            let childNode = this.allTreeNode.children[i]
            if (list[i].saveNum > 0) {
                childNode.active = true
            } else {
                childNode.active = false
            }
        }
    }
    caiZhaiBtnClick(event, customData) {
        let list = User.getBaiCaoJiList()
        let callFunc = () => {
            list[customData].saveNum = 0
            User.setBaiCaoJiList(list)
            this.freshHuaContent(list)
        }
        let arr = []

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

        let temp = 1
        // if (mainId == MainTaskIdEnum.MainTask_593) {
        //     temp = 10
        // }

        // let configData = TaskManager.getTaskConfigById(mainId)
        // let GMGoodIdArray = configData.GMGoodIdArray
        // let taskCaiGouDan = configData.taskCaiGouDan
        // if (taskCaiGouDan && taskCaiGouDan.length > 0 && configData.GMGameType && configData.GMGameType.includes('佰草集')) {
        //     if (this.isVideo) {//当前已经看过广告
        //         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.isVideo = 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
        //             }
        //         }
        //     }
        // }

        arr.push({ goodId: list[customData].goodId, goodNum: temp })
        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(arr, () => {
                callFunc()
                this.checkTask()
            });
        })
    }
    caiZhaiShuBtnClick(event, customData) {

        let list = User.getGuShuList()
        let callFunc = () => {
            list[customData].saveNum = 0
            User.setGuShuList(list)
            this.freshShuContent(list)
        }
        let arr = []

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

        let temp = 1
        // if (mainId == MainTaskIdEnum.MainTask_593) {
        //     temp = 10
        // }

        // 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('佰草集')) {
            if (this.isVideo) {//当前已经看过广告
                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.isVideo = 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
                    }
                }
            }
        }

        arr.push({ goodId: list[customData].goodId, goodNum: temp })
        PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
            prefabNode.getComponent(GetAward).initView(arr, () => {
                callFunc()
                this.checkTask()
            });
        })
    }

    checkTask() {
        let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        let mainId = mainTaskInfo.Id
        // if (mainId == MainTaskIdEnum.MainTask_555) {
        //     let isAllGoods = BagManager.isHaveAllGood([3722, 3723, 3724, 3725, 3726, 3727, 3728, 3729])
        //     if (isAllGoods) {
        //         // TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_593)

        //     }
        // }
        // else if (mainId == MainTaskIdEnum.MainTask_594) {
        //     let isAllGoods = BagManager.isHaveAllGood([1781])
        //     if (isAllGoods) {
        //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_594)

        //         ChatManager.setSingleChatStatus('DH', 'DH_LuoQingCheng', 'DH_27', false)
        //         User.setCurJuQingUnLockIndex('DH_27')
        //         User.setXiaoYanZhanChangStatus(3)
        //         TaskManager.setCurTask(MainTaskIdEnum.MainTask_594_1)
        //     }
        // } else if (mainId == MainTaskIdEnum.MainTask_598) {
        //     let isAllGoods = BagManager.isHaveAllGood([1789])
        //     if (isAllGoods) {
        //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_598)

        //     }
        // }
    }

    refreshBtnClick() {
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-佰草集重新进货`);
                    this.isVideo = true
                    let list = User.getBaiCaoJiList()
                    list.length = 0
                    this.refrehHuaView(true)

                    let list2 = User.getGuShuList()
                    list2.length = 0
                    this.refrehShuView(true)
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }
            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    shuoMingBtnAdClick() {
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-佰草集说明`)
                    // Common5.ReportDY("inLevel", '佰草集-AD-说明');
                    User.setBaiCaoJiShuoMingUnlock(true)
                    this.refreshBtnStatus()
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }
            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }
    shuoMingBtnNoAdClick() {
        PrefabManage.loadPrefabByType(GameType.BaiCaoJiShuoMing)
    }
    refreshBtnStatus() {
        let bUnLock = User.getBaiCaoJiShuoMingUnlock()
        if (bUnLock) {
            this.noAdBtn.active = true
            this.adBtn.active = false
        } else {
            this.noAdBtn.active = false
            this.adBtn.active = true
        }
    }

    refreshZhaoMuTime() {
        let localZhaoMuData = MeiNvZhaoMuManager.getMeiNvZhaoMuData()
        let maxTime = localZhaoMuData[ZhaoMuGameType.QiZhenYuan].zhaoMuTime
        let zhaoMuBeiShu = localZhaoMuData[ZhaoMuGameType.QiZhenYuan].zhaoMuBeiShu
        // let maxUnlockIndex = localZhaoMuData[ZhaoMuGameType.SongHuaJiang].maxUnlockIndex

        if (maxTime > 0) {
            //按钮上人物显示第一个

            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('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`
        }

    }
    zhaomuClickEvent() {
        //Common5.playEffect("sound/按键点击")
        this.enterReportKey(GameType.MeiNvZhaoMu)
        PrefabManage.loadPrefabByType(GameType.MeiNvZhaoMu, null, (prefabNode) => {
            prefabNode.getComponent(MeiNvZhaoMu).initView(ZhaoMuGameType.SongHuaJiang)
        })
    }
}