package in.gov.digilocker.uidai.logs.modal;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import in.gov.digilocker.uidai.logs.constants.OTPAuthCollection;
import in.gov.digilocker.uidai.logs.constants.OTPCollection;
import in.gov.digilocker.uidai.logs.constants.Utils;

@Component
public class OTPAuth {
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

	private String uniqueReqId;
	private String uidToken;
	private String aadhaarNumber;
	private String type;
	private String otp;
	private String fpEnabled;
	private String authType;
	
	
	private boolean status;
	private String response;
	private double avgResTime;
	private String consent;
	private String errCode;
	private String errMsg;
	private String createDate;
	private String serverIP;
	
	public OTPAuth() {		
		this.uniqueReqId = null;
		this.uidToken= null;
		this.aadhaarNumber = null;
		this.otp = null;
		this.fpEnabled= "N";
		this.type = null;
		this.authType = null;
		this.status = false;
		this.response = null;
		this.avgResTime = 0.0;
		this.consent = null;
		this.errCode = null;
		this.errMsg = null;
		this.createDate = null;
		this.serverIP= null;
	}

	public String getUniqueReqId() {
		return uniqueReqId;
	}

	public void setUniqueReqId(String uniqueReqId) {
		this.uniqueReqId = uniqueReqId;
	}
	
	public String getUidToken() {
		return uidToken;
	}

	public void setUidToken(String uidToken) {
		this.uidToken = uidToken;
	}

	public String getAadhaarNumber() {
		return aadhaarNumber;
	}

	public void setAadhaarNumber(String aadhaarNumber) {
		this.aadhaarNumber = aadhaarNumber;
	}

	public String getOtp() {
		return otp;
	}

	public void setOtp(String otp) {
		this.otp = otp;
	}	

	public String getFpEnabled() {
		return fpEnabled;
	}

	public void setFpEnabled(String fpEnabled) {
		this.fpEnabled = fpEnabled;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getAuthType() {
		return authType;
	}

	public void setAuthType(String authType) {
		this.authType = authType;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public String getResponse() {
		return response;
	}

	public void setResponse(String response) {
		this.response = response;
	}

	public double getAvgResTime() {
		return avgResTime;
	}

	public void setAvgResTime(double avgResTime) {
		this.avgResTime = avgResTime;
	}

	public String getConsent() {
		return consent;
	}

	public void setConsent(String consent) {
		this.consent = consent;
	}

	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public String getCreateDate() {
		return null!= createDate ? createDate : dateFormat.format(new Date());
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public String getServerIP() {
		return serverIP;
	}

	public void setServerIP(String serverIP) {
		this.serverIP = serverIP;
	}

	public JSONObject toJSONObject() {
		JSONObject jsonObject = new JSONObject();
		try {
			String aadhaarNum= Utils.getMd5(this.getAadhaarNumber());
			jsonObject.put(OTPAuthCollection.UNIQUE_REQUEST_ID, this.getUniqueReqId());
			jsonObject.put(OTPAuthCollection.UID_TOKEN, this.getUidToken());
			jsonObject.put(OTPAuthCollection.AADHAAR_NUMBER, aadhaarNum);
			jsonObject.put(OTPAuthCollection.TYPE, this.getType());
			jsonObject.put(OTPAuthCollection.AUTH_TYPE, this.getAuthType());
			jsonObject.put(OTPAuthCollection.STATUS, this.isStatus());
			jsonObject.put(OTPAuthCollection.RESPONSE, this.getResponse());
			jsonObject.put(OTPAuthCollection.AVG_RES_TIME, this.getAvgResTime());
			jsonObject.put(OTPAuthCollection.RESIDENTIAL_CONSENT, this.getConsent());
			jsonObject.put(OTPAuthCollection.ERROR_CODE, this.getErrCode());
			jsonObject.put(OTPAuthCollection.ERROR_MSG, this.getErrMsg());
			jsonObject.put(OTPAuthCollection.SERVER_IP, this.getServerIP());
			JSONObject obj= new JSONObject();
			obj.put("$date", this.getCreateDate());			
			jsonObject.put(OTPCollection.CREATE_DATE, obj);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}
}
