import {PanelBaseControl} from "../Control/PanelBaseControl";
import {ViewAction} from "../utils/ViewAction";
import { ViewManager } from "../View/ViewManager";
import { AlertUtils } from "../utils/AlertUtils";

export class WarnPanel extends PanelBaseControl 
{
    static key:string = "WarnPanel";

    titleStr:string = "";
	descStr:string = "";
    type:number = 1;
    cancelStr:string;
    okstr:string;
    callobj:any;
    callback:Function;
    calldata:any=null;

    text_desc:fairygui.GTextField;
    btn_ok:fairygui.GButton;
    btn_cancel:fairygui.GButton;
    c_type:fairygui.Controller;
    constructor( d:any ) 
    { 
        super( "main","alertview",true,ViewAction.ActionType_none,1 ); 
        this.titleStr = d.titleStr;
        this.descStr = d.descStr;
        this.type = d.type;
        this.callobj = d.callobj;
        this.callback = d.callback;
        this.calldata = d.calldata;
        this.cancelStr = d.cancelStr;
        this.okstr = d.okstr;
    }

    protected onInit():void
    {
        super.onInit();
    }

    protected initView():void
    {
        this.c_type = this.getpanelController( "c_type" );
        this.text_desc.text = this.descStr;
        if( this.type == 1 )
        {
            this.c_type.selectedIndex = 1;
        }
        else
        {
            this.c_type.selectedIndex = 0;
        }
    }

    protected initEvent():void
    {
        this.AddEventByType( this.btn_cancel,Laya.Event.CLICK,this,this.cancelclick );
        this.AddEventByType( this.btn_ok,Laya.Event.CLICK,this,this.okclick );
    }

    private cancelclick():void
    {
        this.tocallback( AlertUtils.NO );
        this.onClose();
    }
    private okclick():void
    {
        this.tocallback( AlertUtils.YES );
        this.onClose();
    }

    private closeclick():void
    {
        this.tocallback( AlertUtils.CLOSE );
        this.onClose();
    }

    private tocallback( type:string ):void
    {
        if( this.callobj && this.callback )
        {
            if( this.calldata )
            {
                this.callback.call( this.callobj,type,this.calldata );
            }
            else
            {
                this.callback.call( this.callobj, type );
            }
        }
    }

    private onClose():void
    {
        ViewManager.Instance.close(WarnPanel );
    }

    public destory():void
    {
        super.destory();
    }
}