package com.dbs.consentServer.controller;

import java.util.ArrayList;
import java.util.HashMap;

import com.dbs.consentServer.DTO.saveFormDTO.SaveConsentFormDTO;
import com.dbs.consentServer.service.SaveFormService;
import com.google.common.util.concurrent.ExecutionError;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@Api(tags = { "3. SaveFormService " })
@RestController
@RequestMapping("/SaveFormSvc")
public class SaveFormSvc {
  private final Logger logger = LoggerFactory.getLogger(this.getClass());

  @Autowired
  public SaveFormService saveFormService;

  @RequestMapping(path = "saveTempData", method = RequestMethod.POST)
  @Transactional(rollbackFor = Exception.class)
  public ArrayList<HashMap<String, Object>> saveTempData(SaveConsentFormDTO saveConsentFormDTO) {
    ArrayList<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> map = new HashMap<String, Object>();
    try {
      boolean isNewForm = false;
      String consentMstRid = saveConsentFormDTO.getConsentMstRid();
      String consentState  = saveConsentFormDTO.getConsentState();
      String formXml = saveConsentFormDTO.getFormXml();
      String dataXml = saveConsentFormDTO.getDataXml();

      byte[] formConsentByte = formXml.getBytes("UTF-8");
      byte[] dataConsentByte = dataXml.getBytes("UTF-8");

      saveConsentFormDTO.setFormConsentByte(formConsentByte);
      saveConsentFormDTO.setDataConsentByte(dataConsentByte);
  
      if (consentState.equals("CERTIFY_CMP")) {
        saveConsentFormDTO.setCompleteYn("Y");
        saveConsentFormDTO.setCompleteDateTimeToChars("TO_CHAR(SYSDATE,'YYYY-MM-DD hh24:mi:ss')");
      } else {
        saveConsentFormDTO.setCompleteYn("N");
        saveConsentFormDTO.setCompleteDateTimeToChars("");
      }
  
      if (consentMstRid.equals("0")) {
        isNewForm = true;
      }
  
      if (isNewForm == true) {
        saveConsentFormDTO.setConsentMstRid(String.valueOf(getSequence("CONSENT_MST")));
        saveConsentFormDTO.setConsentDataRid(String.valueOf(getSequence("CONSENT_DATA")));
        saveFormService.insertConsentMaster(saveConsentFormDTO);
        saveFormService.insertConsentData(saveConsentFormDTO);

        consentMstRid = saveConsentFormDTO.getConsentMstRid();
      } else {
  
      }

      map.put("code", "00");
      map.put("data", consentMstRid);
      result.add(map);
    } catch (Exception ex) {
      logger.error("saveTemp Exception -- > " + ex);
      map.put("code", "01");
      result.add(map);
    }

    return result;
  }

  private int getSequence(String code) {
    int seq = -1;
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("subCd", code);

    seq = saveFormService.getSequence(params);

    if (seq == 0) {
      seq = saveFormService.setSequence(params);
    } else {
      params.put("ud1Num", ++seq);
      saveFormService.updateSequence(params);
    }

    return seq;
  }
}