package com.bpa.scalage.HBL.Result;


import com.bpa.scalage.HBL.Interface.IRunT;
import com.bpa.scalage.HBL.Logs.MessageLog;

public class OperateResultT<T> extends OperateResult
{
    public OperateResultT(Exception ex)
    {
        super(ex);
    }

    public OperateResultT(String msg, ResultCode rc )
    {
        super(msg, rc);
    }

    public OperateResultT(ResultCode rc)
    {
        super(rc);
    }
    public OperateResultT()
    {
        super();
    }
    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);
    }
}
