import i18n from "../../../../i18n/i18n";
import FDialogAttr, { DialogAttrContent } from "./FDialogAttr";

/**
 * 战斗中对话框
 */
const {ccclass, property} = cc._decorator;
/**
 * 对话内容
 */
export interface DialogContent{
    name:string,
    name1:string,
    icon:string,
    content:string
}


@ccclass
export default class FDialogBox extends cc.Component {

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

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

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

    @property(cc.Sprite)
    mIcon: cc.Sprite = null;

    @property(cc.Node)
    mDialogNode: cc.Node = null;//对话框节点

    @property(cc.Node)
    mAttrNode: cc.Node = null;//属性选择节点

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

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

    /**
     * 下标
     */
    private tmpDialogIndex = 0;
    /**
     * 临时的对话信息
     */
    private tmpDialogs:Array<DialogContent> = null;
    /**
     * 对话的回调
     */
    private callback:()=>void;

    /**
     * 选择属性的回调
     */
    private attrCallback:(type:DialogAttrContent)=>void;

    public onclick(){
        this.tmpDialogIndex ++;
        if(this.tmpDialogs && this.tmpDialogIndex < this.tmpDialogs.length){
            let data = this.tmpDialogs[this.tmpDialogIndex];
            this.setDialog(data);
        }else{
            if(this.callback){
                this.callback();
                this.callback = null;
            }
        }
    }
    public setCallback(callback:()=>void){
        this.callback = callback;
    }

    public setData(tmpDialogs:Array<DialogContent>){
        this.mDialogNode.active = true;
        this.mAttrNode.active = false;
        this.tmpDialogIndex = 0;
        this.tmpDialogs = tmpDialogs;
        let data = this.tmpDialogs[0];
        this.setDialog(data);
    }
    /**
     * 设置对话内容
     * @param data 
     */
    private setDialog(data:DialogContent){
        this.mName.string = i18n.t(data.name)
        this.mName1.string = i18n.t(data.name1)
        this.mContent.string = i18n.t(data.content)
        cc.resources.load('icon/npc/'+data.icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
            if(err){
                cc.error(err);
            }else{
               this.mIcon.spriteFrame = spriteFrame;
            }
        } );
    }

    public setAttrCallback(callback:(data)=>void){
        this.attrCallback = callback;
    }
    public setAttr(datas:Array<DialogAttrContent>){
        this.mDialogNode.active = false;
        this.mAttrNode.active = true;

        for (let i = 0; i < datas.length; i++) {
            const element = datas[i];
            let node = cc.instantiate(this.mAttrPerfab);
            node.parent = this.mAttrContent;
            let attr = node.getComponent(FDialogAttr);
            attr.setVaule(element);
            attr.setCallback((da:FDialogAttr)=>{
                if(this.attrCallback){
                    this.attrCallback(da.attr);
                }
            });
        }
    }





}
