import i18n from "../../../i18n/i18n";
import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import GoodItem from "../../common/GoodItem";
import FFCalAttr from "../../data/FFCalAttr";
import { __GodCardLevel } from "../../data/sdata/SManage";
import CardInfoView from "./CardInfoView";
import CardStarSuccessView from "./CardStarSuccessView";
import CardAttrItem from "./item/CardAttrItem";
import CardItem from "./item/CardItem";
import CardUplevelItem from "./item/CardUplevelItem";

/**
 * 神像升星
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class CardStarView extends ViewObject {

    public static attrName = ['攻击','防御','气血','敏捷','水','火','风','雷','土'];

    @property(CardItem)
    mCardItem: CardItem = null;

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

    @property(cc.Prefab)
    mAttrPrefab: cc.Prefab = null;

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

    @property(cc.Prefab)
    mGoodItemPrefab: cc.Prefab = null;

    @property([CardUplevelItem])
    mUpLevelItem: Array<CardUplevelItem> = [];

    @property(cc.Node)
    mFullStar: cc.Node = null;//满星显示

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

    @property(cc.Node)
    mFinish: cc.Node = null;//所有操作完成后显示

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

    private upStatus = 0;

    public cardInfoView:CardInfoView
    private tempStar = 0;

    public exitDistroy(){
        super.exitDistroy()
        this.cardInfoView.flushRadarChart()
    }

    public init(cardInfoView:CardInfoView){
        this.cardInfoView = cardInfoView

        let attrValues = [0,0,0,0,0,0,0,0,0]

        let _godCard = cardInfoView.cardItem._godCard
        if(_godCard.godReborn){
            let _godReborn = _godCard.godReborn
            if(_godReborn){
                attrValues = [_godReborn.atk,_godReborn.def,_godReborn.hp,
                    _godReborn.sp,_godReborn.water,_godReborn.fire,
                    _godReborn.wind,_godReborn.thunder,_godReborn.earth]
            }
        }
        for (let i = 0; i < attrValues.length; i++) {
            let v1 = attrValues[i];
            if(v1 > 0){
                let node = cc.instantiate(this.mAttrPrefab)
                node.parent = this.mFullContent
                let cardAttrItem:CardAttrItem = node.getComponent(CardAttrItem)
                
                let jobStr = ''
                if(i < 4){
                    let job = ['','射手','法师','战士']
                    jobStr = job[_godCard.job]
                }
                cardAttrItem.init(jobStr+CardStarView.attrName[i],v1,0)
            }
        }
        this.mFullStar.active = false;

        this.flushView()
    }

    public flushView(){
        let cardItem:CardItem = this.cardInfoView.cardItem
        let star = this.mCardItem.init(this.main,cardItem.godCardAttr)
        let isUpdateView = true
        if(this.tempStar == 0){
            this.tempStar = star
        }else{
            if(this.tempStar != star){
                isUpdateView = false
                this.tempStar = star
                this.showSuccess(()=>{
                    this.updateView()
                })
            }
        }
        if(isUpdateView){
            this.updateView()
        }
    }
    private updateView(){
        let cardItem:CardItem = this.cardInfoView.cardItem
        let fAttr = FFCalAttr.getGodCardAttr(this.main,cardItem.godCardAttr)
        let _godCardLevel:__GodCardLevel = cardItem._godCard.list[cardItem.godCardAttr.level]//下一级属性
        let _godCardLevel1:__GodCardLevel = null//当前属性
        if(cardItem.godCardAttr.level > 0){
            _godCardLevel1 = cardItem._godCard.list[cardItem.godCardAttr.level - 1]
        }
        
        let attrValues = [fAttr.atk,fAttr.def,fAttr.hp,fAttr.sp,fAttr.water,
            fAttr.fire,fAttr.wind,fAttr.thunder,fAttr.earth]
        let attrValues1 = [0,0,0,0,0,0,0,0,0]
        let attrValues2 = [0,0,0,0,0,0,0,0,0]
        if(_godCardLevel){
            attrValues1 = [_godCardLevel.atk,_godCardLevel.def,_godCardLevel.hp,
                _godCardLevel.sp,_godCardLevel.water,_godCardLevel.fire,
                _godCardLevel.wind,_godCardLevel.thunder,_godCardLevel.earth]
        }
        if(_godCardLevel1){
            attrValues2 = [_godCardLevel1.atk,_godCardLevel1.def,_godCardLevel1.hp,
                _godCardLevel1.sp,_godCardLevel1.water,_godCardLevel1.fire,
                _godCardLevel1.wind,_godCardLevel1.thunder,_godCardLevel1.earth]
        }

        this.mAttrContent.destroyAllChildren()
        for (let i = 0; i < attrValues.length; i++) {
            let v1 = attrValues[i];
            let v2 = attrValues1[i] - attrValues2[i]
            if(v1 > 0 || v2 > 0){
                let node = cc.instantiate(this.mAttrPrefab)
                node.parent = this.mAttrContent
                let cardAttrItem:CardAttrItem = node.getComponent(CardAttrItem)

                let jobStr = ''
                if(i < 4){
                    let job = ['','射手','法师','战士']
                    jobStr = job[cardItem._godCard.job]
                }
                cardAttrItem.init(jobStr+CardStarView.attrName[i],v1,v2)
            }
        }
        let goods = []
        if(_godCardLevel){
            if(_godCardLevel.goodId1 > 0){
                goods.push({
                    id:_godCardLevel.goodId1,
                    count:_godCardLevel.goodCount1
                })
            }
    
            if(_godCardLevel.goodId2 > 0){
                goods.push({
                    id:_godCardLevel.goodId2,
                    count:_godCardLevel.goodCount2
                })
            }
            if(_godCardLevel.goodId3 > 0){
                goods.push({
                    id:_godCardLevel.goodId3,
                    count:_godCardLevel.goodCount3
                })
            }
            this.mFullStar.active = false
            this.mBtName.string = i18n.t('升星')
            this.upStatus = 0
        }else{
            this.upStatus = 1
            this.mBtName.string = i18n.t('重塑')
            this.mFullStar.active = true
            let _godCard = this.cardInfoView.cardItem._godCard
            let _godReborn = _godCard.godReborn

            if(_godReborn.goodId1 > 0){
                goods.push({
                    id:_godReborn.goodId1,
                    count:_godReborn.goodCount1
                })
            }
    
            if(_godReborn.goodId2 > 0){
                goods.push({
                    id:_godReborn.goodId2,
                    count:_godReborn.goodCount2
                })
            }
            if(this.cardInfoView.cardItem.godCardAttr.reborn > 0){
                this.mFinish.active = false
            }
        }

        this.mGoodContent.destroyAllChildren()
        for (let i = 0; i < goods.length; i++) {
            const element = goods[i];
            let node = cc.instantiate(this.mGoodItemPrefab)
            let goodItem = node.getComponent(GoodItem)
            goodItem.initContrast(this.main,element)
            node.parent = this.mGoodContent
        }
        
        this.flushTree()
    }

    private flushTree(){
        let _godCard = this.cardInfoView.cardItem._godCard

        let tree = []
        let _cardLevels = _godCard.list
        let tmpId = 0;
        let tmpList = []
        for (let i = 0; i < _cardLevels.length; i++) {
            const element = _cardLevels[i];
            if(tmpId == 0){
                tmpId = element.star
                tmpList.push(element)
            }else if(tmpId != element.star){
                tmpId = element.star
                let list = tmpList.slice()
                tree.push(list)
                tmpList = []
                tmpList.push(element)
            }else{
                tmpList.push(element)
            }
        }
        tree.push(tmpList)

        let godCardAttr = this.cardInfoView.cardItem.godCardAttr
        let _godCardLevel = _godCard.list[godCardAttr.level]
        if(_godCardLevel){
            let myStar = _godCardLevel.star

            let myList = tree[myStar - 1]
            
            let tmpData = this.getTmpLevel(_godCardLevel,myList)
            
            for (let i = 0; i < 6; i++) {
                const element = this.mUpLevelItem[i];
                if(i > tmpData.index){
                    element.setLock()
                }else if(i == tmpData.index){
                    element.setLabel(tmpData.level,tmpData.maxLevel)
                }else{
                    element.setLabel(tmpData.maxLevel,tmpData.maxLevel)
                }
            }
        }
    }
    /**
     * 获取在某个节点上的等级
     * @param _godCardLevel 
     * @param list 
     */
    private getTmpLevel(_godCardLevel:__GodCardLevel,list:Array<__GodCardLevel>){
        let maxLevel = (list.length - 1)/5;//当前最大等级
        if(_godCardLevel == list[list.length - 1]){//如果是最后一个，不会分数升级
            return {
                index:5,//当前所在位置
                level:0,//当前所在位置等级
                maxLevel:maxLevel//最高等级
            }
        }
        let level = 0;
        for (let i = 0; i < list.length - 1; i++) {
            const element = list[i];
            if(_godCardLevel == element){
                return {
                    index:element.attrType - 1,//当前所在位置
                    level:level,//当前所在位置等级
                    maxLevel:maxLevel//最高等级
                }
            }
            level ++
            if(level >= maxLevel){
                level = 0
            }
        }
    }

    /**
     * 升星
     */
    public onclickStar(){
        let action = ''
        if(this.upStatus == 0){
            action = 'card/v1/uplevel'
        }else{
            action = 'card/v1/reborn'
        }
        let cardItem = this.cardInfoView.cardItem
        let msg = {
            id:cardItem.godCardAttr.id
        }
        this.main.gameHttp.sendJson(action,msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    if(this.upStatus == 0){
                        cardItem.godCardAttr.level += 1
                    }else{
                        cardItem.godCardAttr.reborn += 1
                    }
                    this.flushView()
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }
    /**
     * 显示进阶成功
     */
    public showSuccess(callback:()=>void){
        let count = 0
        this.schedule(()=>{
            count += 0.1
            this.mUpLevelItem[5].mProgessBar.progress = count
            if(count > 1){
                this.main.viewManage.loadFunc(GameViewType.god_card_star_success,(viewObject:ViewObject)=>{
                    let cardInfoView = viewObject as CardStarSuccessView
                    cardInfoView.init(this.cardInfoView.cardItem)
            
                    viewObject.show();
                });
                callback()
            }
        },0.1,10)
    }


}
