import GameReport from "../../FrameWork/Report/ZyZyReport";
import User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import TaskManager from "../JuQingChat/TaskManager";
import BagManager, { GoodsProperty, GoodsLocalProperty } from "../Manager/BagManager";
import LevelUpManager from "../Manager/LevelUpManager";
import ShipuManager from "../Manager/ShipuManager";
import TanWeiManager from "../Manager/TanWeiManager";
import UserManager from "../Manager/UserManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ZhaoHuanTai from "../ZhaoHuanTai/ZhaoHuanTai";
import GetAward from "./GetAward";

const { ccclass, property } = cc._decorator;

export enum GetAward_BtnStatus {
    ChuShou,
    BeiBao,
    BuYao,
    LingQu
}
@ccclass
export default class GetDiaoLuo extends cc.Component {

    @property(cc.Node)
    diaoluo_1: cc.Node = null;
    @property(cc.Node)
    diaoluo_2: cc.Node = null;

    @property(cc.Sprite)
    icon_1: cc.Sprite = null;
    @property(cc.Label)
    name_1: cc.Label = null;
    @property(cc.RichText)
    shouyi: cc.RichText = null;

    @property(cc.Sprite)
    icon_2_1: cc.Sprite = null;
    @property(cc.Sprite)
    icon_2_2: cc.Sprite = null;
    @property(cc.Sprite)
    dikuang_2_1: cc.Sprite = null;
    @property(cc.Sprite)
    dikuang_2_2: cc.Sprite = null;
    @property(cc.Label)
    name_2_1: cc.Label = null;
    @property(cc.Label)
    name_2_2: cc.Label = null;
    @property(cc.Label)
    shouyiname_2_1: cc.Label = null;
    @property(cc.Label)
    shouyiname_2_2: cc.Label = null;
    @property(cc.Label)
    shouyi_2_1: cc.Label = null;
    @property(cc.Label)
    shouyi_2_2: cc.Label = null;

    @property(cc.Label)
    allGetNum: cc.Label = null;

    @property(cc.RichText)
    shouyiUp: cc.RichText = null;

    goodId: any;
    diaoluoData: any;

    levelBgName = ['物品-灰_', '物品-灰_', '物品-绿', '物品-蓝', '物品-紫', '物品-橙', '物品-彩']
    levelName = ['粗糙', '粗糙', '普通', '高级', '优秀', '极品', '神话']

    m_index = 0

    start() {
        this.diaoluo_1.active = true
        this.diaoluo_2.active = false
        Common5.playRemoteAudioEffect("remotesound/effect/掉落弹窗")
        cc.sys.localStorage.setItem("掉落引导", '1')
    }

    initView(diaoluo, callFunc?) {
        this.diaoluoData = diaoluo
        // if (typeof (diaoluo.wuping) == "number") {
        // this.goodId = diaoluo.wuping
        // Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + this.goodId, this.icon_1)
        // let configDate = BagManager.getBagGoodConfig(this.goodId)
        // this.diaoluo_1.getChildByName("道具名称").getChildByName("name").getComponent(cc.Label).string = configDate.goodName
        // } else {
        // let configDate
        // let id = this.diaoluoData.wuping.id
        // if (diaoluo.wuping.isShiPu) {
        //     configDate = ShipuManager.getShipuConfigDate(id)
        //     let beishu = ShipuManager.getShipuInComeByIdAndLevel(id, diaoluo.pingzhi)
        //     this.shouyi.string = `<color=#ffffff>秒赚收益:</c><color=#FFF19D>x${beishu}倍</color>`

        //     Common5.addUrlSprite_custom('shipuIcon/' + configDate.name, this.icon_1)
        // } else {
        //     configDate = TanWeiManager.getShipuConfigDate(id)
        //     let beishu = TanWeiManager.getShipuInComeByIdAndLevel(id, diaoluo.pingzhi)
        //     this.shouyi.string = `<color=#ffffff>点击收益:</c><color=#FFF19D>x${beishu}倍</color>`

        //     Common5.addUrlSprite_custom('tanweiTexture/icon/' + configDate.name, this.icon_1)
        // }
        // this.name_1.string = configDate.name
        // }

        //全都要剩余次数
        //新版本强制到3
        if (User.getDiaoLuoAllGet() > 5) {
            User.setDiaoLuoAllGet(3)
        }
        this.allGetNum.string = `${User.getDiaoLuoAllGet()}/3`

        if (User.getDiaoLuoAllGet() <= 0) {
            this.diaoluo_1.getChildByName("替换").getComponent(cc.Button).interactable = false
        }

        for (let i = 0; i < diaoluo.length; i++) {
            let _diaoluo = diaoluo[i]
            if (_diaoluo.wuping.isShiPu || _diaoluo.wuping.isTanWei) {
                let configDate
                let id = _diaoluo.wuping.id
                let node = cc.instantiate(this.diaoluo_1.getChildByName("dikuang"))
                node.active = true
                node.attr({ diaoluoData: _diaoluo })
                this.diaoluo_1.getChildByName("掉落物品").addChild(node)
                let kuang = node.getChildByName("kuang")
                Common5.getSpriteFrameFromBundle("GameRes", 'getDiaoLuo/奖励弹窗/' + this.levelBgName[_diaoluo.pingzhi], kuang.getComponent(cc.Sprite))
                let icon = node.getChildByName("kuang").getChildByName("icon")
                if (_diaoluo.wuping.isShiPu) {
                    configDate = ShipuManager.getShipuConfigDate(id)
                    let beishu = ShipuManager.getShipuInComeByIdAndLevel(id, _diaoluo.pingzhi)
                    // this.shouyi.string = `<color=#ffffff>秒赚收益:</c><color=#FFF19D>x${beishu}倍</color>`

                    Common5.addUrlSprite_custom('shipuIcon/' + configDate.name, icon.getComponent(cc.Sprite))

                    node.getChildByName("name").getComponent(cc.Label).string = configDate.name + `(${this.levelName[_diaoluo.pingzhi]})`
                    node.getChildByName("shouyi").getComponent(cc.RichText).string = `<color=#ffffff><outline color=#000000 width=2>秒赚收益:</c><color=#FFF19D><outline color=#000000 width=2>x${beishu}倍</color>`
                } else {
                    configDate = TanWeiManager.getShipuConfigDate(id)
                    let beishu = TanWeiManager.getShipuInComeByIdAndLevel(id, _diaoluo.pingzhi)
                    // this.shouyi.string = `<color=#ffffff>点击收益:</c><color=#FFF19D>x${beishu}倍</color>`

                    Common5.addUrlSprite_custom('tanweiTexture/icon/' + configDate.name, icon.getComponent(cc.Sprite))

                    node.getChildByName("name").getComponent(cc.Label).string = configDate.name + `(${this.levelName[_diaoluo.pingzhi]})`
                    node.getChildByName("shouyi").getComponent(cc.RichText).string = `<color=#ffffff><outline color=#000000 width=2>点击收益:</c><color=#FFF19D><outline color=#000000 width=2>x${beishu}倍</color>`
                }

                if (_diaoluo.wuping.isAD) {
                    node.getChildByName("视频图标").active = true
                } else {
                    node.getChildByName("视频图标").active = false
                }
            } else if (_diaoluo.wuping.isZhuZi) {//珠子
                let node = cc.instantiate(this.diaoluo_1.getChildByName("dikuang"))
                node.active = true
                node.attr({ diaoluoData: _diaoluo })
                node.getChildByName("视频图标").active = false
                this.diaoluo_1.getChildByName("掉落物品").addChild(node)
                let kuang = node.getChildByName("kuang")
                Common5.getSpriteFrameFromBundle("GameRes", 'getDiaoLuo/奖励弹窗/' + this.levelBgName[2], kuang.getComponent(cc.Sprite))
                let icon = node.getChildByName("kuang").getChildByName("icon")
                Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + _diaoluo.wuping.id, icon.getComponent(cc.Sprite))
                // node.getChildByName("shouyi").active = false
                node.getChildByName("name").getComponent(cc.Label).string = BagManager.getJuQingGoodConfig(_diaoluo.wuping.id).goodName
                node.getChildByName("shouyi").getComponent(cc.RichText).string = `<color=#ffffff><outline color=#000000 width=2>集齐7颗可以开出神秘大奖</c>`

            } else if (_diaoluo.wuping.isQian) {
                let node = cc.instantiate(this.diaoluo_1.getChildByName("dikuang"))
                node.active = true
                node.attr({ diaoluoData: _diaoluo })
                node.getChildByName("视频图标").active = false
                this.diaoluo_1.getChildByName("掉落物品").addChild(node)
                let kuang = node.getChildByName("kuang")
                Common5.getSpriteFrameFromBundle("GameRes", 'getDiaoLuo/奖励弹窗/' + this.levelBgName[_diaoluo.pingzhi], kuang.getComponent(cc.Sprite))
                let icon = node.getChildByName("kuang").getChildByName("icon")
                Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + _diaoluo.wuping.id, icon.getComponent(cc.Sprite))
                // node.getChildByName("shouyi").active = false
                node.getChildByName("name").getComponent(cc.Label).string = '钱袋子' + `(${this.levelName[_diaoluo.pingzhi]})`
                let money = LevelUpManager.levelUpSpned() * (_diaoluo.pingzhi + 1)
                let moneyStr = Common5.getNumberChangeHanzi(money)
                node.getChildByName("shouyi").getComponent(cc.RichText).string = `<color=#ffffff><outline color=#000000 width=2>金额:</c><color=#FFF19D><outline color=#000000 width=2>${moneyStr}</color>`
                if (_diaoluo.wuping.isAD) {
                    node.getChildByName("视频图标").active = true
                } else {
                    node.getChildByName("视频图标").active = false
                }
            } else if (_diaoluo.wuping.isDengJi) {
                let node = cc.instantiate(this.diaoluo_1.getChildByName("dikuang"))
                node.active = true
                node.attr({ diaoluoData: _diaoluo })
                node.getChildByName("视频图标").active = false
                this.diaoluo_1.getChildByName("掉落物品").addChild(node)
                let kuang = node.getChildByName("kuang")
                Common5.getSpriteFrameFromBundle("GameRes", 'getDiaoLuo/奖励弹窗/' + this.levelBgName[_diaoluo.pingzhi], kuang.getComponent(cc.Sprite))
                let icon = node.getChildByName("kuang").getChildByName("icon")
                Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + _diaoluo.wuping.id, icon.getComponent(cc.Sprite))
                // node.getChildByName("shouyi").active = false
                node.getChildByName("name").getComponent(cc.Label).string = '等级提升' + `(${this.levelName[_diaoluo.pingzhi]})`
                node.getChildByName("shouyi").getComponent(cc.RichText).string = `<color=#ffffff><outline color=#000000 width=2>提升:</c><color=#FFF19D><outline color=#000000 width=2>${_diaoluo.pingzhi}</color>`
                if (_diaoluo.wuping.isAD) {
                    node.getChildByName("视频图标").active = true
                } else {
                    node.getChildByName("视频图标").active = false
                }
            }
        }

        this.node.scale = 0.1
        cc.tween(this.node)
            .to(1.0, { scale: 1 }, { easing: 'elasticOut' })
            .start()
    }

    onTouchCancel_1() {
        this.node.removeFromParent()
        this.node.destroy()
    }

    onTouchCancel_2() {
        this.diaoluo_1.active = true
        this.diaoluo_2.active = false
    }

    showTiHuan() {
        this.diaoluo_1.active = false


        // Common5.addUrlSprite_custom("GoodIcon/texture/icon/" + this.goodId, this.icon_2_1)
        // let goodData = BagManager.getGoodsProperty(this.goodId)

        let maxindex = this.diaoluoData.length
        if (this.m_index < maxindex) {
            let diaoluo = this.diaoluoData[this.m_index]
            this.showData(diaoluo, false)
        }

        if (this.m_index == 0) {
            let getDiaoLuoAllGet = User.getDiaoLuoAllGet()
            getDiaoLuoAllGet--
            User.setDiaoLuoAllGet(getDiaoLuoAllGet)
        }
    }

    onTouchTiHuan() {
        let tab = {
            onClose: (finish) => {
                if (finish) {
                    User.setShowAdNum(User.getShowAdNum() + 1)
                    Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-掉落看视频`)
                    // Common5.ReportDY("inLevel", '掉落-AD-掉落看视频');
                    this.showTiHuan()
                }
                else {
                    Common5.showTips_custom("广告未观看完");
                }
            }, onFailed: () => {

            }
        }
        AppPlatform.playVideo_custom(tab)

    }

    showData(diaoluo, isone) {
        if (diaoluo.wuping.isShiPu || diaoluo.wuping.isTanWei) {//物品
            let id = diaoluo.wuping.id
            if (diaoluo.wuping.isShiPu) {
                let configDate = ShipuManager.getShipuConfigDate(id)
                let userDate = ShipuManager.getShipuUserDate(id)
                this.name_2_1.string = configDate.name + `(${this.levelName[userDate.level]})`
                this.name_2_2.string = configDate.name + `(${this.levelName[diaoluo.pingzhi]})`
                Common5.addUrlSprite_custom('shipuIcon/' + configDate.name, this.icon_2_1)
                Common5.addUrlSprite_custom('shipuIcon/' + configDate.name, this.icon_2_2)

                Common5.getSpriteFrameFromBundle('GameRes', 'getDiaoLuo/奖励弹窗/' + this.levelBgName[userDate.level], this.dikuang_2_1)
                Common5.getSpriteFrameFromBundle('GameRes', 'getDiaoLuo/奖励弹窗/' + this.levelBgName[diaoluo.pingzhi], this.dikuang_2_2)

                let beishu1 = ShipuManager.getShipuInComeCurLevel(id)
                let beishu2 = ShipuManager.getShipuInComeByIdAndLevel(id, diaoluo.pingzhi)
                this.shouyiname_2_1.string = "秒赚收益:"
                this.shouyiname_2_2.string = "秒赚收益:"
                this.shouyi_2_1.string = `${beishu1}倍`
                this.shouyi_2_2.string = `${beishu2}倍`
                let cha = ShipuManager.getShipuInComeCha(id, diaoluo.pingzhi, userDate.level)
                this.shouyiUp.string = `<color=#ffffff>秒赚收益永久提升:</c><color=#FFDC3C>${cha}倍</color>`
                this.shouyiUp.node.getChildByName("箭头").getComponent(cc.Widget).updateAlignment();
            } else {
                let configDate = TanWeiManager.getShipuConfigDate(id)
                let userDate = TanWeiManager.getShipuUserDate(id)
                this.name_2_1.string = configDate.name + `(${this.levelName[userDate.level]})`
                this.name_2_2.string = configDate.name + `(${this.levelName[diaoluo.pingzhi]})`
                Common5.addUrlSprite_custom('tanweiTexture/icon/' + configDate.name, this.icon_2_1)
                Common5.addUrlSprite_custom('tanweiTexture/icon/' + configDate.name, this.icon_2_2)

                Common5.getSpriteFrameFromBundle('GameRes', 'getDiaoLuo/奖励弹窗/' + this.levelBgName[userDate.level], this.dikuang_2_1)
                Common5.getSpriteFrameFromBundle('GameRes', 'getDiaoLuo/奖励弹窗/' + this.levelBgName[diaoluo.pingzhi], this.dikuang_2_2)

                let beishu1 = TanWeiManager.getShipuInComeCurLevel(id)
                let beishu2 = TanWeiManager.getShipuInComeByIdAndLevel(id, diaoluo.pingzhi)
                this.shouyiname_2_1.string = "点击收益:"
                this.shouyiname_2_2.string = "点击收益:"
                this.shouyi_2_1.string = `${beishu1}倍`
                this.shouyi_2_2.string = `${beishu2}倍`
                let cha = TanWeiManager.getShipuInComeCha(id, diaoluo.pingzhi, userDate.level)
                this.shouyiUp.string = `<color=#ffffff>点击收益永久提升:</c><color=#FFDC3C>${cha}倍</color>`
                this.shouyiUp.node.getChildByName("箭头").getComponent(cc.Widget).updateAlignment();
            }
            this.diaoluo_2.active = true
        } else if (diaoluo.wuping.isZhuZi) {//珠子
            let zhuzis = [3730, 3731, 3732, 3733, 3734, 3735, 3736]
            if (zhuzis.includes(diaoluo.wuping.id)) {
                let bagConfig: GoodsLocalProperty = {
                    goodId: diaoluo.wuping.id,
                    goodNum: 1,
                }
                BagManager.addBagList(bagConfig)
                PrefabManage.loadPrefabByType(GameType.ZhaoHuanTai, null, (node) => {
                    node.getComponent(ZhaoHuanTai).init(diaoluo.wuping.id, () => {
                        let maxindex = this.diaoluoData.length
                        this.m_index++
                        if (this.m_index < maxindex && !isone) {
                            this.showTiHuan()
                        } else {
                            this.node.removeFromParent()
                            this.node.destroy()
                        }
                    })
                })
            }
            this.diaoluo_2.active = false
        } else if (diaoluo.wuping.isQian) {
            PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                prefabNode.getComponent(GetAward).initView([{ goodId: diaoluo.wuping.id, goodNum: 1 }], () => {
                    let money = LevelUpManager.levelUpSpned() * (diaoluo.pingzhi + 1)
                    UserManager.addMoney(money)
                    let maxindex = this.diaoluoData.length
                    this.m_index++
                    if (this.m_index < maxindex && !isone) {
                        this.showTiHuan()
                    } else {
                        this.node.removeFromParent()
                        this.node.destroy()
                    }
                });
            })
            this.diaoluo_2.active = false
        } else if (diaoluo.wuping.isDengJi) {
            PrefabManage.loadPrefabByType(GameType.GetAward, null, (prefabNode) => {
                prefabNode.getComponent(GetAward).initView([{ goodId: diaoluo.wuping.id, goodNum: 1 }], () => {
                    LevelUpManager.peopleLevelUp(diaoluo.pingzhi)
                    let maxindex = this.diaoluoData.length
                    this.m_index++
                    if (this.m_index < maxindex && !isone) {
                        this.showTiHuan()
                    } else {
                        this.node.removeFromParent()
                        this.node.destroy()
                    }
                });
            })
            this.diaoluo_2.active = false
        }
    }

    onTouchSure() {
        let _diaoluo = this.diaoluoData[this.m_index]
        let id = _diaoluo.wuping.id
        if (_diaoluo.wuping.isShiPu) {
            let userDate = ShipuManager.getShipuUserDate(id)
            ShipuManager.shipuLevelUpById(_diaoluo.wuping.id, _diaoluo.pingzhi - userDate.level)
        } else {
            let userDate = TanWeiManager.getShipuUserDate(id)
            TanWeiManager.shipuLevelUpById(_diaoluo.wuping.id, _diaoluo.pingzhi - userDate.level)
        }
        let maxindex = this.diaoluoData.length
        this.m_index++
        if (this.m_index < maxindex) {
            this.showTiHuan()
        } else {
            this.node.removeFromParent()
            this.node.destroy()
        }
    }

    onTouch(event) {
        let node = event.target
        if (node.getChildByName("视频图标").active) {
            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        User.setShowAdNum(User.getShowAdNum() + 1)
                        Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-掉落看视频`)
                        // Common5.ReportDY("inLevel", '掉落-AD-掉落看视频');
                        this.diaoluo_1.active = false
                        this.diaoluoData = [node.diaoluoData]
                        this.showData(node.diaoluoData, true)
                    }
                    else {
                        Common5.showTips_custom("广告未观看完");
                    }
                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)
        } else {
            this.diaoluo_1.active = false
            this.diaoluoData = [node.diaoluoData]
            this.showData(node.diaoluoData, true)
        }
    }

    onCloseAward() {
        this.node.removeFromParent()
        this.node.destroy()
    }

}