// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../FrameWork/Event/EventEnum"
import EventMgr from "../../FrameWork/Event/EventMgr"
import User from "../../FrameWork/User/User"
import BagManager from "./BagManager"



let tanweiConfig = [
    { id: 0, addPercent: 2.0, curLevel: 1, gameName: '', name: '擀面杖', unlLock1: 200, needShipuId: -9999, gameType: null, unLockMoney: 20000000, levelUpSpnedMoney: 3000000, inComeMultiple: 2 },
    { id: 1, addPercent: 3.0, curLevel: 1, gameName: '', name: '砧板', unlLock1: 300, needShipuId: -9999, gameType: null, unLockMoney: 300000000, levelUpSpnedMoney: 3000000, inComeMultiple: 3 },
    { id: 2, addPercent: 4.0, curLevel: 1, gameName: 'xx', name: '面锅', unlLock1: 400, needShipuId: -9999, gameType: null, unLockMoney: 10000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 4 },
    { id: 3, addPercent: 5.0, curLevel: 1, gameName: '', name: '漏勺', unlLock1: 500, needShipuId: -9999, gameType: null, unLockMoney: 120000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 5 },
    { id: 4, addPercent: 15.0, curLevel: 1, gameName: '', name: '汤勺', unlLock1: 600, needShipuId: -9999, gameType: null, unLockMoney: 5000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 15 },
    { id: 5, addPercent: 20.0, curLevel: 1, gameName: '', name: '摆摊车', unlLock1: 700, needShipuId: -9999, gameType: null, unLockMoney: 60000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 20 },
    { id: 6, addPercent: 25.0, curLevel: 1, gameName: 'xx', name: '调料盒', unlLock1: 800, needShipuId: -9999, gameType: null, unLockMoney: 500000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 25 },
    { id: 7, addPercent: 50.0, curLevel: 1, gameName: '', name: '长筷子', unlLock1: 900, needShipuId: -9999, gameType: null, unLockMoney: 30000000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 50 },
    { id: 8, addPercent: 60.0, curLevel: 1, gameName: '', name: '抹布', unlLock1: 1200, needShipuId: -9999, gameType: null, unLockMoney: 100000000000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 60 },
    { id: 9, addPercent: 240.0, curLevel: 1, gameName: 'xx', name: '煤气灶', unlLock1: 1500, needShipuId: -9999, gameType: null, unLockMoney: 40000000000000000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 240 },
    { id: 10, addPercent: 480.0, curLevel: 1, gameName: 'xx', name: '招牌', unlLock1: 1800, needShipuId: -9999, gameType: null, unLockMoney: 4000000000000000000000000000, levelUpSpnedMoney: 3000000, inComeMultiple: 480 },

]


//食谱
export default class TanWeiManager {

    public static getManagerConfigDate() {
        return tanweiConfig

    }

    public static initManager() {
        let array = User.getTanweiArray()
        if (array.length == 0) {
            //插入数据
            let tempArray = []
            for (let i = 0; i < tanweiConfig.length; i++) {
                let obj = { id: 0, isLock: true, level: 0 }
                obj.id = tanweiConfig[i].id
                obj.isLock = true
                obj.level = 0
                tempArray[tempArray.length] = obj
            }
            User.setTanweiArray(tempArray)
        } else {

        }
    }

    public static getShipuUserDate(id) {
        let array = User.getTanweiArray()
        for (let i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                return array[i]
            }
        }
        return null
    }

    public static getShipuConfigDate(id) {
        for (let i = 0; i < tanweiConfig.length; i++) {
            if (tanweiConfig[i].id == id) {
                return tanweiConfig[i]
            }
        }
        return null
    }

    public static shipuUnLockById(id) {
        let array = User.getTanweiArray()

        for (let i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                array[i].isLock = false
                array[i].level = 1
                break
            }
        }
        User.setTanweiArray(array)
        //发送通知
        EventMgr.emitEvent_custom(ryw_Event.unlockItem)
    }

    public static shipuLevelUpById(id, num = 1) {
        let array = User.getTanweiArray()
        let maxLevel = 6
        for (let i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                array[i].level += num

                if (array[i].level >= maxLevel) {
                    array[i].level = maxLevel
                }

                break
            }
        }
        User.setTanweiArray(array)
        EventMgr.emitEvent_custom(ryw_Event.levelUpitem)
    }

    public static shipuIsMaxLevel(id) {
        let array = User.getTanweiArray()
        let maxLevel = 6
        for (let i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                if (array[i].level >= maxLevel) {
                    return true
                } else {
                    return false
                }
            }
        }

    }

    public static getShipuClickInCome(id) {
        let array = User.getTanweiArray()
        for (let i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                if (array[i].isLock) {
                    return false
                } else {
                    return true
                }

            }
        }
    }

    //点击收益
    public static getAllClickInCome() {
        let allNum = 0
        let array = User.getTanweiArray()
        for (let i = 0; i < array.length; i++) {
            if (array[i].isLock == false) {
                let config = this.getShipuConfigDate(array[i].id)
                allNum += (config.addPercent * 100)
            }
        }

        return allNum / 100
    }

    public static haveMoneyLockItem() {
        let isLock = false
        for (let i = 0; i < tanweiConfig.length; i++) {
            let userDate = this.getShipuUserDate(tanweiConfig[i].id)
            if (userDate && userDate.isLock && this.getCurShipuCanUnLock(tanweiConfig[i].id) == 'all') {
                if (User.getMoney() - tanweiConfig[i].unLockMoney >= 0) {
                    isLock = true
                    break
                }
            }
        }
        if (isLock) {
            return isLock
        }
        // //升级
        // let maxLevel = 6
        // for (let i = 0; i < tanweiConfig.length; i++) {
        //     let userDate = this.getShipuUserDate(tanweiConfig[i].id)
        //     if (userDate.isLock == false && userDate.level < maxLevel) {
        //         let spend = this.levelUpShipuSpendMoney(tanweiConfig[i].id)
        //         if (User.getMoney() - spend >= 0) {
        //             isLock = true
        //             break
        //         }
        //     }
        // }
        return isLock
    }

    public static getCurShipuCanUnLock(id) {
        let config = this.getShipuConfigDate(id)

        let myLevel = User.getMyLevel()

        let unLockLevel = config.unlLock1
        let needShipuId = config.needShipuId

        let condition1 = false
        if (myLevel >= unLockLevel) {
            condition1 = true
        }

        let condition2 = false
        if (needShipuId == -9999) {
            condition2 = true
        } else {
            if (BagManager.getBagGoodNums(needShipuId) > 0) {
                condition2 = true
            }
        }

        if (condition1 && condition2) {
            return 'all'
        }
        else {
            if (!condition1) {
                return 'myLevel'
            } else {
                return 'caipu'
            }

        }

    }


    public static levelUpShipuSpendMoney(id) {
        let config = this.getShipuConfigDate(id)
        let userDate = this.getShipuUserDate(id)
        let unLockMoney = config.unLockMoney
        let level = userDate.level
        let maxLevel = 6
        if (level >= maxLevel) {
            level = maxLevel
        }

        let xishu = 1.1
        let spendMoney = Math.pow(level, 1.1) * xishu
        spendMoney = Math.floor(unLockMoney * spendMoney)

        return spendMoney
    }

    public static getShipuInComeNextLevel(id) {
        let config = this.getShipuConfigDate(id)
        let userDate = this.getShipuUserDate(id)

        let inComeMultiple = config.inComeMultiple
        let level = userDate.level
        let maxLevel = 6
        let nextLevel = level + 1
        if (nextLevel >= maxLevel) {
            nextLevel = maxLevel
        }

        // let num = inComeMultiple + (nextLevel * 0.5)
        let xishu = 1.2
        let powNum = 1
        if (nextLevel > 1) {
            powNum = Math.pow(xishu, nextLevel)
        }
        let num = inComeMultiple * powNum
        return Math.floor(num * 10) / 10


    }

    public static getShipuInComeCurLevel(id) {
        let config = this.getShipuConfigDate(id)
        let userDate = this.getShipuUserDate(id)

        let inComeMultiple = config.inComeMultiple
        let level = userDate.level
        if (level == 0) {
            return 0
        }
        let maxLevel = 6
        if (level >= maxLevel) {
            level = maxLevel
        }
        // let num = inComeMultiple + (level * 0.5)
        // return Math.floor(num * 10) / 10
        let xishu = 1.2
        let powNum = 1
        if (level > 1) {
            powNum = Math.pow(xishu, level)
        }
        let num = inComeMultiple * powNum
        return Math.floor(num * 10) / 10
    }

    //获取当前id的某个等级提升的倍数
    public static getShipuInComeByIdAndLevel(id, _level) {
        let config = this.getShipuConfigDate(id)

        let inComeMultiple = config.inComeMultiple
        let level = _level
        if (level == 0) {
            return 0
        }
        let maxLevel = 6
        if (level >= maxLevel) {
            level = maxLevel
        }
        // let num = inComeMultiple + (level * 0.5)
        let xishu = 1.2
        let powNum = 1
        if (level > 1) {
            powNum = Math.pow(xishu, level)
        }
        let num = inComeMultiple * powNum
        return Math.floor(num * 10) / 10

    }

    //获取当前id的两个等级之间的相差倍数
    public static getShipuInComeCha(id, level1, level2) {
        // let num = (Math.abs(level2 - level1) * 0.5)
        let config = this.getShipuConfigDate(id)
        let inComeMultiple = config.inComeMultiple
        let xishu = 1.2
        let powNum1 = 1
        if (level1 > 1) {
            powNum1 = Math.pow(xishu, level1)
        }
        let powNum2 = 1
        if (level2 > 1) {
            powNum2 = Math.pow(xishu, level2)
        }
        let num = inComeMultiple * powNum1 - inComeMultiple * powNum2
        return Math.floor(num * 10) / 10

    }

    public static getAllShipuInCome() {
        let allNum = 0
        let array = User.getTanweiArray()
        for (let i = 0; i < array.length; i++) {
            let num = this.getShipuInComeCurLevel(array[i].id)
            allNum += num
        }

        if (allNum == 0) {
            return 1
        }
        return allNum
    }


    public static getLockItemIndex() {
        let index = 0
        let isLock = false
        for (let i = 0; i < tanweiConfig.length; i++) {
            let userDate = this.getShipuUserDate(tanweiConfig[i].id)
            if (userDate && userDate.isLock && this.getCurShipuCanUnLock(tanweiConfig[i].id) == 'all') {
                if (User.getMoney() - tanweiConfig[i].unLockMoney >= 0) {
                    isLock = true
                    index = i
                    break
                }
            }
        }
        if (isLock) {
            return index
        }
        // //升级
        // let maxLevel = 6
        // for (let i = 0; i < tanweiConfig.length; i++) {
        //     let userDate = this.getShipuUserDate(tanweiConfig[i].id)
        //     if (userDate.isLock == false && userDate.level < maxLevel) {
        //         let spend = this.levelUpShipuSpendMoney(tanweiConfig[i].id)
        //         if (User.getMoney() - spend >= 0) {
        //             isLock = true
        //             index = i

        //         }
        //     }
        // }
        return index

    }

    public static getUnLockNum() {
        let array = User.getTanweiArray()
        let num = 0
        for (let i = 0; i < array.length; i++) {
            if (!array[i].isLock) {
                num++
            }
        }
        return num
    }

    public static getLockById(id) {
        let array = User.getTanweiArray()
        let isLock = true
        for (let i = 0; i < array.length; i++) {
            if (array[i].id == id) {
                isLock = array[i].isLock
            }
        }
        return isLock
    }
}