import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import CardInfoView from "./CardInfoView";
import CardStarSuccessView from "./CardStarSuccessView";
import CardItem from "./item/CardItem";

/**
 * 神庙
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class GodCardView extends ViewObject {

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

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

    public onLoad() {
        this.init(1)
    }
    
    public init(type:number){
        this.mContent.destroyAllChildren()
        let godCard = this.main.player.godCard
        let _godCards = this.main.sManage.godCards
        for (let i = 0; i < _godCards.length; i++) {
            const element = _godCards[i];
            if(element.type == type){
                let godCardAttr = godCard[element.id]
                let node = cc.instantiate(this.mCardItem)
                node.parent = this.mContent
                let cardItem = node.getComponent(CardItem)
                if(godCardAttr){
                    cardItem.init(this.main,godCardAttr)
                    cardItem.setCallback((item:CardItem)=>{
                        this.openGodCard(item)
                    })
                }else{
                    cardItem.initNo(this.main,element)
                    cardItem.setCallback((item:CardItem)=>{
                        this.getGodCard(item)
                    })
                }
            }
        }

        for (const id in godCard) {
            let godCardAttr = godCard[id]
            let _godCard = this.main.sManage.getGodCardById(godCardAttr.id)
            if(_godCard.type == type){
                
            }
        }
    }

    public onclickType(event,data){
        let type = parseInt(data)
        this.init(type)
    }

    /**
     * 神卡说明
     */
    public openGodCard(cardItem:CardItem){
        this.main.viewManage.loadFunc(GameViewType.god_card_info,(viewObject:ViewObject)=>{

            let cardInfoView = viewObject as CardInfoView
            cardInfoView.init(cardItem)

            viewObject.show();
        });
    }
    public getGodCard(cardItem:CardItem){
        let msg = {
            id:cardItem._godCard.id
        }
        this.main.gameHttp.sendJson('card/v1/exchange',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    let godCard = reve.data._godCard[0]
                    cardItem.init(this.main,godCard)
                    this.main.showReward(reve)
                    cardItem.setCallback((item)=>{
                        this.openGodCard(item)
                    })
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });

    }
   
}
