package com.bonait.bnframework.HBL.Result;

import com.bonait.bnframework.HBL.Interface.IRun;
import com.bonait.bnframework.HBL.Interface.IRunT;
import com.bonait.bnframework.HBL.Logs.MessageLog;

public class OperateResult extends IResult {

    public OperateResult(){

    }

    public OperateResult(Exception ex){
        message = ex.getMessage()+"\rException:"+ex.toString();
        resultCode = ResultCode.Exception;
        isSuccess=false;
    }

    public OperateResult(String msg, ResultCode rc)
    {
        message = msg;
        resultCode = rc;
        isSuccess=false;
    }

    public OperateResult(ResultCode rc)
    {
        resultCode = rc;
        isSuccess=true;
    }


    @Override
    public void setErrorMsg(String msg) {
        message =message+ msg;
        isSuccess=false;
        resultCode = ResultCode.Fail;
    }

    public OperateResult OnSource(IRun action){
        if(action!=null&&getIsSuccess())action.Run();
        return this;
    }

    public OperateResult OnFailed(IRunT<String> action){
        try{
            if(action!=null&&!getIsSuccess())action.Run(message);
        }catch(Exception e){
            MessageLog.ShowError(e);
        }
        return this;
    }

    public OperateResult OnFinally(IRun action){
        try{
            if(action!=null)action.Run();
        }catch(Exception e){
            MessageLog.ShowError(e);
        }
        return this;
    }

    public static OperateResult CreateSuccess()
    {
        return new OperateResult(ResultCode.Success);
    }

    public static OperateResult CreateFailed(String msg)
    {
        MessageLog.ShowError(msg);
        return new OperateResult(msg, ResultCode.Fail);
    }

    public static OperateResult CreateFailed(Exception ex)
    {
        MessageLog.ShowError(ex.toString());
        return new OperateResult(ex);
    }
}
