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

import com.seeyon.aicloud.common.JsonUtils;
import com.seeyon.apps.esign.constants.JsonParamKeyConstants;
import com.seeyon.apps.esign.po.signer.OrgInfo;
import com.seeyon.apps.esign.po.signer.PersonInfo;
import com.seeyon.apps.esign.po.signer.SignParty;
import com.seeyon.apps.esign.po.signer.SignerType;
import com.seeyon.apps.esign.service.SignParamSource;
import com.seeyon.apps.esign.utils.CallbackUrlBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JsonParamSource implements SignParamSource {

    private Map<String,Object> oriParams;
    private String callbackBaseUrl;
    private List<SignParty> parties;
    private String appId;
    public JsonParamSource(){}
    public JsonParamSource(String jsonStr){
        this.oriParams = JsonUtils.parseObject(jsonStr,Map.class);
    }
    public JsonParamSource(Map<String,Object> params){
        this.oriParams = params;
    }

    @Override
    public String getContractTitle() {
        return (String)oriParams.get(JsonParamKeyConstants.CONTRACT_TITLE);
    }

    @Override
    public boolean isByFile() {
        return (Boolean) oriParams.get(JsonParamKeyConstants.BY_FILE);
    }

    @Override
    public String getBizFormId() {
        return (String)oriParams.get(JsonParamKeyConstants.FORM_ID);
    }

    @Override
    public String getTableName() {
        return (String)oriParams.get(JsonParamKeyConstants.TABLE_NAME);
    }

    @Override
    public String getSignFileRefId() {
        return (String)oriParams.get(JsonParamKeyConstants.SIGN_FILE_REF_ID);
    }

    @Override
    public String getSignedFileField() {
        return (String)oriParams.get(JsonParamKeyConstants.SIGNED_FILE_FIELD);
    }

    @Override
    public String getSignStatusField() {
        return (String)oriParams.get(JsonParamKeyConstants.SIGN_STATUS_FIELD);
    }

    @Override
    public Long getSignStatusEnumId() {
        return oriParams.get(JsonParamKeyConstants.SIGN_STATUS_ENUM_ID) == null ? null : (Long) oriParams.get(JsonParamKeyConstants.SIGN_STATUS_ENUM_ID);
    }

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

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

    @Override
    public List<SignParty> getSignerParties() {
        String tempStr = (String)oriParams.get(JsonParamKeyConstants.SIGNERS);
        List signers = JsonUtils.parseObject(tempStr, List.class);
        List<SignParty> parties = new ArrayList<>();
        Float qfPos = 100f;
        for (Object signer : signers) {
            Map<String,Object> signerMap = (Map<String,Object>) signer;
            SignParty signParty = new SignParty();
            SignerType signerType = SignerType.valueOf((String) signerMap.get(JsonParamKeyConstants.SIGNER_TYPE));
            signParty.setSignerType(signerType);
            signParty.setSignOrder((Integer) signerMap.get(JsonParamKeyConstants.SIGN_ORDER));
            signParty.setAutoSign(false);
            signParty.setFreeSign(true);
            signParty.setQfPos(qfPos);
            qfPos += 100f;
            signParty.setPartyId((String)signerMap.get(JsonParamKeyConstants.NAME));
            if(signerMap.get(JsonParamKeyConstants.SIGN_POS_KEYWORD) != null) {
                signParty.setSignKeyword((String)signerMap.get(JsonParamKeyConstants.SIGN_POS_KEYWORD));
                signParty.setFreeSign(false);
            }
            if(signerType == SignerType.PERSON) {
                PersonInfo personInfo = new PersonInfo();
                personInfo.setName((String)signerMap.get(JsonParamKeyConstants.NAME));
                personInfo.setPhone((String)signerMap.get(JsonParamKeyConstants.PHONE));
                signParty.setPersonInfo(personInfo);
            }else {
                OrgInfo orgInfo = new OrgInfo();
                PersonInfo transactor = new PersonInfo();
                orgInfo.setTransactor(transactor);
                orgInfo.setOrgName((String)signerMap.get(JsonParamKeyConstants.ORG_NAME));
                orgInfo.setOrgCode((String)signerMap.get(JsonParamKeyConstants.ORG_CODE));
                transactor.setName((String)signerMap.get(JsonParamKeyConstants.NAME));
                transactor.setPhone((String)signerMap.get(JsonParamKeyConstants.PHONE));
                signParty.setOrgInfo(orgInfo);
            }
            parties.add(signParty);
        }
    return parties;
    }

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

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

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

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