package com.dbs.mplus.fatima.consent.sign;

import android.os.AsyncTask;

import com.dbs.mplus.fatima.consent.webservice.ConsentWSWrapper;
import com.dbs.mplus.fatima.framework.processor.msg.ConsentSignMessage;

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

/**
 * Created by dbs on 2016-10-27.
 */
public class BatchSignTask extends AsyncTask<Void, Void, Boolean> {

    private ConsentSignMessage msg;

    public BatchSignTask(ConsentSignMessage msg) {
        this.msg = msg;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        String userId = msg.getUserId();
        List<Map<String, String>> consentSignMapList = msg.getConsentSignMapList();
        for(Map<String, String> consentSignMap : consentSignMapList) {
            int consentMstRid = Integer.parseInt(consentSignMap.get("consentMstRid"));
            int consentImageRid = Integer.parseInt(consentSignMap.get("consentImageRid"));
            String certTarget = consentSignMap.get("certTarget");
            // 서명
            String certResult = SignWrapper.getInstance().signData(userId, certTarget);
            if(certResult != null) {
                try {
                    ConsentWSWrapper.getInstance().saveImageSignInfo(consentMstRid, consentImageRid, userId, certTarget, certResult);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;
    }
}
