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 RoleFrameView 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.roleFrameView = this
        this.iconInfoView.openType = 2
        this.init()
    }

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

        for (let i = 0; i < roleIcons.length; i++) {
            const element = roleIcons[i];
            if(element.type == 2){
                let node = cc.instantiate(this.mFrameIcon)
                let frameIcon = node.getComponent(RoleIconItem)
                frameIcon.roleIcon = element
                frameIcon.mIcon.node.active = false
                frameIcon.loadFrame(this.main)
                frameIcon.flushLock(this.main)
                node.parent = this.mContent
                frameIcon.setCallback((fip:RoleIconItem)=>{
                    this.openInfo(fip)
                })
                if(frameId == element.id){
                    fip = frameIcon
                }
            }
        }
        if(!fip){
            fip = this.mContent.children[0].getComponent(RoleIconItem)
        }
        this.flush(fip)
    }
    public flush(fip:RoleIconItem){
        this.iconInfoView.initFrame(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 && element < 300){
                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.frame){
                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;
            }
        }
    }
}