package com.seeyon.apps.esign.po.param;

import com.seeyon.apps.esign.config.EsignConfigProvider;
import com.seeyon.apps.esign.constants.FlowFormFieldConstants;
import com.seeyon.apps.esign.po.signer.*;
import com.seeyon.apps.esign.service.SignParamSource;
import com.seeyon.apps.esign.utils.CallbackUrlBuilder;
import com.seeyon.apps.ext.workflow.vo.FieldDataVo;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.cap4.form.bean.FormDataMasterBean;

import java.util.List;

public class FlowParamSource implements SignParamSource {

    private FormDataVo formDataVo;
    private FormDataMasterBean masterBean;
    private Boolean doAutoSign = false;
    private EsignConfigProvider configProvider;
    private String callbackBaseUrl;
    private String appId;
    private List<SignParty> signerParties;
    private Boolean useKeyWordPositing = true;

    public FormDataVo getFormDataVo() {
        return formDataVo;
    }

    public void setFormDataVo(FormDataVo formDataVo) {
        this.formDataVo = formDataVo;
    }

    public FormDataMasterBean getMasterBean() {
        return masterBean;
    }

    public void setMasterBean(FormDataMasterBean masterBean) {
        this.masterBean = masterBean;
    }

    public FlowParamSource(){}

    public FlowParamSource(FormDataVo formDataVo, FormDataMasterBean masterBean){
        this.formDataVo = formDataVo;
        this.masterBean = masterBean;
    }

    public Boolean getDoAutoSign() {
        return doAutoSign;
    }

    public void setDoAutoSign(Boolean doAutoSign) {
        this.doAutoSign = doAutoSign;
    }

    @Override
    public String getContractTitle() {
        return getStringField(formDataVo, FlowFormFieldConstants.CONTRACT_TITLE);
    }

    @Override
    public boolean isByFile() {
        return true;
    }

    @Override
    public String getBizFormId() {
        return formDataVo.getToken() +"_" + formDataVo.getId();
    }

    @Override
    public String getTableName() {
        return masterBean.getFormTable().getTableName();
    }

    @Override
    public String getSignFileRefId() {
        return getStringField(formDataVo, FlowFormFieldConstants.CONTRACT_APPROVAL_ATTACHMENT);
    }

    @Override
    public String getSignedFileField() {
        return masterBean.getFormTable().getFieldBeanByDisplay(FlowFormFieldConstants.SIGNED_ATTACHMENT).getColumnName();
    }

    @Override
    public String getSignStatusField() {
        return masterBean.getFormTable().getFieldBeanByDisplay(FlowFormFieldConstants.SIGN_STATUS).getColumnName();
    }

    @Override
    public Long getSignStatusEnumId() {
        return masterBean.getFormTable().getFieldBeanByDisplay(FlowFormFieldConstants.SIGN_STATUS).getEnumId();
    }

    @Override
    public boolean autoSign() {
        return doAutoSign;
    }

    @Override
    public String getCallbackUrl() {
        return CallbackUrlBuilder.build(callbackBaseUrl, "FLOW",
                getBizFormId(), getTableName(),
                getSignedFileField(), getSignStatusField(),
                getSignStatusEnumId(), getAppId());
    }

    public void setSignerParties(List<SignParty> signerParties) {
        this.signerParties = signerParties;
    }

    @Override
    public List<SignParty> getSignerParties() throws Exception {
      return signerParties;
    }


    @Override
    public String noticeTypes() {
        return SignParamSource.super.noticeTypes();
    }

    @Override
    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    private String getStringField(FormDataVo vo, String field) {
        try {
            FieldDataVo fieldData = vo.getFieldData(field);
            return fieldData.getStringValue();
        }catch (Exception e) {
            return null;
        }
    }

    public EsignConfigProvider getConfigProvider() {
        return configProvider;
    }

    public void setConfigProvider(EsignConfigProvider configProvider) {
        this.configProvider = configProvider;
    }

    public void setCallbackBaseUrl(String callbackBaseUrl) {
        this.callbackBaseUrl = callbackBaseUrl;
    }

    @Override
    public boolean useKeyWordPositioning() {
        return useKeyWordPositing;
    }

    public String getCallbackBaseUrl() {
        return callbackBaseUrl;
    }

    public Boolean getUseKeyWordPositing() {
        return useKeyWordPositing;
    }

    public void setUseKeyWordPositing(Boolean useKeyWordPositing) {
        this.useKeyWordPositing = useKeyWordPositing;
    }
}
