package com.bonait.bnframework.HBL.Result;


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

public class OperateResultT<T> extends IResult
{
    public OperateResultT(){}
    public OperateResultT(Exception ex) {
        message = ex.getMessage()+"\rException:"+ex.toString();
        resultCode = ResultCode.Exception;
        isSuccess=false;
    }

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

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

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

    public OperateResultT(T content, String msg, ResultCode rc){
        Content = content;
        message = msg;
        resultCode = rc;
        isSuccess=getIsSuccess();
    }

    public T Content ;

    public void setContent(T value){
        Content = value;
        resultCode = ResultCode.Success;
        isSuccess=true;
    }

    public  OperateResultT<T> OnSource(IRunT<OperateResultT<T>> action){
        try{
            if(action!=null&&getIsSuccess())action.Run(this);
        }catch(Exception e){
            MessageLog.ShowError(e);
        }
        return this;
    }

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

    public OperateResultT<T> OnFinally(IRunT<OperateResultT<T>> action){
        if(action!=null)action.Run(this);
        return this;
    }

    public  static <T>  OperateResultT<T>  CreateSuccess(T value)
    {
        return new OperateResultT<T>(value,"", ResultCode.Success);
    }

    public static <T> OperateResultT<T> CreateFailedT(String msg)
    {
        MessageLog.ShowError(msg);
        return new OperateResultT<T>( msg, ResultCode.Fail);
    }

    public static <T> OperateResultT<T> CreateFailedT(Exception ex)
    {
        MessageLog.ShowError(ex.toString());
        return new OperateResultT<T>(ex);
    }
}
