import { GameViewType } from "../../../main/ViewManage";
import ViewObject from "../../../main/ViewObject";
import FFCalAttr from "../../data/FFCalAttr";
import TapRole from "../TapRole";
import RoleIconItem from "./RoleIconItem";
import IconInfoView from "./IconInfoView";

/**
 * 头像设置界面
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class RoleIconView extends ViewObject {

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

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

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

    @property(IconInfoView)
    private iconInfoView:IconInfoView = null

    public tapRole:TapRole = null;

    onLoad(){
        this.iconInfoView.main = this.main
        this.iconInfoView.roleIconView = this
        this.iconInfoView.openType = 1
        this.init()
    }

    private init(){
        let roleIcons = this.main.sManage.getRoleIcon()
        let fip = undefined
        let iconId = this.main.player.role.icon

        for (let i = 0; i < roleIcons.length; i++) {
            const element = roleIcons[i];
            if(element.type == 1){
                let node = cc.instantiate(this.mFrameIcon)
                let frameIcon = node.getComponent(RoleIconItem)
                frameIcon.roleIcon = element
                frameIcon.flush(this.main)
                node.parent = this.mContent
                frameIcon.setCallback((fip:RoleIconItem)=>{
                    this.openInfo(fip)
                })
                if(iconId == element.id){
                    fip = frameIcon
                }
            }
        }
        if(!fip){
            fip = this.mContent.children[0].getComponent(RoleIconItem)
        }
        this.flush(fip)
    }
    public flush(fip:RoleIconItem){
        this.iconInfoView.init(this.main,fip)
        let data = {
            atk:0,
            def:0,
            hp:0,
            sp:0
        }
        let open = this.main.player.roleIcon.open
        for (let i = 0; i < open.length; i++) {
            const element = open[i];
            if(element < 200){
                let temp = this.main.sManage.getRoleIconById(element)
                data.atk += temp.atk
                data.def += temp.def
                data.hp += temp.hp
                data.sp += temp.sp
            }
        }
        let zdl = FFCalAttr.getZdl(data)
        this.mZdl.string = ''+zdl

        let nodes = this.mContent.children
        for (let i = 0; i < nodes.length; i++) {
            const node = nodes[i];
            let item = node.getComponent(RoleIconItem)
            if(item.roleIcon.id != this.main.player.role.icon){
                item.setCheck(false)
            }else{
                item.setCheck(true)
            }
        }
    }

    private openInfo(fip:RoleIconItem){
        this.flush(fip)
        fip.mCheck.active = true
        let nodes = fip.node.parent.children
        for (let i = 0; i < nodes.length; i++) {
            const element = nodes[i];
            let tmp = element.getComponent(RoleIconItem)
            if(tmp != fip){
                tmp.mCheck.active = false;
            }
        }
    }

}
