// 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 AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import UiBase from "../GameBase/UiBase";
import TaskManager from "../JuQingChat/TaskManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MultipleIncome extends UiBase {

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

    @property(cc.Node)
    deng1: cc.Node = null;
    @property(cc.Node)
    deng2: cc.Node = null;
    isAnimEnd = false
    // LIFE-CYCLE CALLBACKS:
    maxTime = 5 * 60
    maxTimeD = 3 * 60
    // onLoad () {}
    isMaxMultiple = false
    amountNum = 0
    onDestroy(): void {
        if (this.isAnimEnd == true) {
            User.setShouyiFanbeiNum(this.amountNum)
            let time = User.getShouyiFanbeiTime()
            if (User.getZhuanPanFree()) {
                User.setZhuanPanFree(false)
                User.setShouyiFanbeiTime(this.maxTimeD + time)
            } else {
                User.setShouyiFanbeiTime(this.maxTime + time)
            }
            EventMgr.emitEvent_custom(ryw_Event.MultipleIncomeEvent)
        }
    }
    start() {
        cc.tween(this.deng1)
            .delay(0.5)
            .call(() => {

                this.deng2.active = false
            })
            .delay(0.5)
            .call(() => {

                this.deng2.active = true
            })
            .union()
            .repeatForever()
            .start()

        PrefabManage.preloadPrefabByType(GameType.BoxTipNode)

        let zhuanpan = User.getZhuanPanFree()

        if (zhuanpan) {
            this.node.getChildByName('videoBtn').active = false
            this.node.getChildByName('FreeBtn').active = true
            let node = this.node.getChildByName('FreeBtn')
            this.guideView([node])

        } else {
            this.node.getChildByName('videoBtn').active = true
            this.node.getChildByName('FreeBtn').active = false


        }

        let index = User.getFirstStepIndex()
        if (index == 4) {
            this.loadPrefabs()
        }


    }


    loadPrefabs() {
        this.scheduleOnce(() => {
            let curSignDay = -1
            let signList = User.getSignList()

            for (let i = 0; i < 7; i++) {
                if (signList[i] == 0) {
                    curSignDay = i
                    break
                }
            }

            let index = User.getFirstStepIndex()
            if (index == 1) {
                let array = [GameType.PeopleLevelUp, GameType.MishuGame, GameType.MultipleIncome]
                PrefabManage.preloadPrefabArray(array)
            } else {
                let array = [GameType.PeopleLevelUp, GameType.MishuGame, GameType.ChallengeChoose, GameType.SignDay, GameType.OnLineGame, GameType.MultipleIncome, GameType.TanWei, GameType.Shipu, GameType.FangChanGame, GameType.GuPiao, GameType.ChatList, GameType.CityPrefab]
                if (curSignDay == -1) {
                    array = [GameType.PeopleLevelUp, GameType.MishuGame, GameType.ChallengeChoose, GameType.OnLineGame, GameType.MultipleIncome, GameType.TanWei, GameType.Shipu, GameType.FangChanGame, GameType.GuPiao, GameType.ChatList, GameType.CityPrefab]
                }

                PrefabManage.preloadPrefabArray(array)
            }

        }, 0)
    }

    guideView(nodeArray, opacity = -1) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if (opacity != -1) {
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }

    closeView() {
        cc.audioEngine.stopAllEffects();
        this.node.removeFromParent()
        this.node.destroy()
        EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, '');
    }

    chouquFreeClick() {
        //Common5.playEffect("sound/按键点击") 
        if (this.isAnimEnd) {

            return
        }

        this.choujiangAnim()
        this.isAnimEnd = true
    }

    chouquAdClick() {
        //Common5.playEffect("sound/按键点击") 
        if (this.isAnimEnd) {

            return
        }

        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-收益翻倍`)
                    // Common5.ReportDY("inLevel", "收益翻倍-AD-翻倍");
                    this.choujiangAnim()
                    this.isAnimEnd = true
                }
                else {

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

            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)
    }

    getRandomNum(min, max) {
        let minNumber = Math.min(min, max);
        let maxNumber = Math.max(min, max);
        return Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber;
    }

    choujiangAnim() {
        let clickTimes = 12; //设置转盘指针多时间
        let clickTimes2 = 12;
        let rounds = -6;	//设置转盘指针多少圈	
        this.turntableBG.stopAllActions()
        this.turntableBG.angle = 0

        let awardAngle01 = parseInt((Math.random() * (-45) + (-25)) + ''); //1   
        let awardAngle02 = parseInt((Math.random() * (-45) + (-95)) + '');  //2
        let awardAngle03 = parseInt((Math.random() * (-45) + (-165)) + ''); //3 
        let awardAngle04 = parseInt((Math.random() * (-45) + (-235)) + '');  //4
        let awardAngle05 = parseInt((Math.random() * (-45) + (-305)) + '');  //5

        var awardMapping = {  // 转盘 会 根据 angleVlue 进行判断,angleVlue 是重要参数,angleVlue 同事涉及对 转盘角度的控制
            0: { amount: '', angleVlue: '0' },
            1: { amount: 2, angleVlue: '1' },
            2: { amount: 10, angleVlue: '2' },
            3: { amount: 8, angleVlue: '3' },
            4: { amount: 5, angleVlue: '4' },
            5: { amount: 3, angleVlue: '5' }
        };
        //////console.log(res.json, 'res')
        let pro = [35, 30, 30, 3, 2]
        let levelUser = User.getMyLevel()
        if (levelUser < 300) {
            pro = [25, 35, 30, 5, 5]
        } else if (levelUser >= 300 && levelUser < 900) {
            pro = [25, 10, 10, 45, 10]
        } else {
            pro = [15, 20, 15, 40, 10]
        }
        let chooseTotal = 5
        let index = Common5.getProbabilityResultIndex(pro)
        let chooseTotalArray = [1, 5, 4, 3, 2]
        chooseTotal = chooseTotalArray[index]
        //     let num_= this.getRandomNum(1, 101)
        //     let chooseTotal = 5
        //    if(num_ <= 20){
        //         chooseTotal = 5

        //     }else if(num_ > 20 && num_ <= 50){
        //         chooseTotal = 1

        //     }else if(num_ > 50 && num_ <= 75){
        //         chooseTotal = 4

        //     }else if(num_ > 75 && num_ <= 95){
        //         chooseTotal = 3

        //     }else if(num_ > 95 && num_ <= 100){
        //         chooseTotal = 2

        //     }

        let zhuanpan = User.getZhuanPanFree()

        if (zhuanpan) {
            chooseTotal = 4

        }


        if (this.isMaxMultiple) {
            chooseTotal = 2
        }

        //1-2
        //2-10
        //3-8
        //4-5
        //5-3

        var adValue = awardMapping[chooseTotal].angleVlue;  //获取 角度 根据 角度来判断
        let amount = awardMapping[chooseTotal].amount
        this.amountNum = amount
        console.log(chooseTotal, amount, 'chooseTotal')
        let self = this
        let inforDelayShow = () => {
            self.scheduleOnce(function () {
                //开启倒计时
                self.isAnimEnd = false
                this.node.active = false
                PrefabManage.loadPrefabByType(GameType.ShouYiTip, null, (prefab) => {
                    prefab.getComponent('ShouYiTip').setViewData(amount, () => {
                        User.setShouyiFanbeiNum(amount)
                        let time = User.getShouyiFanbeiTime()
                        if (User.getZhuanPanFree()) {
                            User.setZhuanPanFree(false)
                            User.setShouyiFanbeiTime(self.maxTimeD + time)
                        } else {
                            User.setShouyiFanbeiTime(self.maxTime + time)
                        }

                        EventMgr.emitEvent_custom(ryw_Event.MultipleIncomeEvent)
                        self.closeView()
                    }, () => {
                        Common5.ReportDY("inLevel", "收益翻倍-AD-10倍");
                        self.isMaxMultiple = true
                        self.choujiangAnim()

                    })
                    // prefab.getComponent('BoxTipScript').setBtnStr('领 取', '抽10倍')
                    // prefab.getComponent('BoxTipScript').setTitleView(true)
                })
                // PrefabManage.loadPrefabByType(GameType.BoxTipNode, null, (prefab) => {
                //     prefab.getComponent('BoxTipScript').setViewData(2, `恭喜抽中${amount}倍收益,所有收益翻倍`, () => {
                //         User.setShouyiFanbeiNum(amount)
                //         let time = User.getShouyiFanbeiTime()
                //         if (User.getZhuanPanFree()) {
                //             User.setZhuanPanFree(false)
                //             User.setShouyiFanbeiTime(self.maxTimeD + time)
                //         } else {
                //             User.setShouyiFanbeiTime(self.maxTime + time)
                //         }

                //         EventMgr.emitEvent_custom(ryw_Event.MultipleIncomeEvent)
                //         self.closeView()
                //     }, () => {
                //         Common5.ReportDY("inLevel", "收益翻倍-AD-10倍");
                //         self.isMaxMultiple = true
                //         self.choujiangAnim()

                //     })
                //     prefab.getComponent('BoxTipScript').setBtnStr('领 取', '抽10倍')
                //     prefab.getComponent('BoxTipScript').setTitleView(true)
                // })
            }, clickTimes2 - 2);

        }
        let curOperateNode = this.turntableBG

        let angle_ = 0
        if (adValue == 1) {
            angle_ = awardAngle01 + 360 * rounds
        } else if (adValue == 2) {
            angle_ = awardAngle02 + 360 * rounds

        } else if (adValue == 3) {
            angle_ = awardAngle03 + 360 * rounds

        } else if (adValue == 4) {
            angle_ = awardAngle04 + 360 * rounds

        } else if (adValue == 5) {
            angle_ = awardAngle05 + 360 * rounds
        }

        Common5.playRemoteAudioEffect('sound/diandiandian/转盘抽奖音效')
        cc.tween(curOperateNode)
            .call(() => {
                this.turntableBG.angle = 0
            })
            .delay(0.1)
            .to(clickTimes, { angle: angle_ }, { easing: 'quintOut' })

            .start()
        inforDelayShow();

    }
    // update (dt) {}
}