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.DemoAuthCollection;
import in.gov.digilocker.uidai.logs.constants.OTPCollection;
import in.gov.digilocker.uidai.logs.constants.Utils;



@Component
public class DemoAuth {

	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

	private String uniqueReqId;
	private String uidToken;
	private String aadhaarNumber;
	private String fullname;
	private String gender;
	private String dob;
	private String phone;
	private String pin;
	private String type;
	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 DemoAuth() {
		this.uniqueReqId = null;
		this.uidToken = null;
		this.aadhaarNumber = null;
		this.fullname = null;
		this.gender = null;
		this.dob = null;
		this.phone = null;
		this.pin = null;
		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 getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getDob() {
		return dob;
	}

	public void setDob(String dob) {
		this.dob = dob;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPin() {
		return pin;
	}

	public void setPin(String pin) {
		this.pin = pin;
	}

	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(DemoAuthCollection.UNIQUE_REQ_ID, this.getUniqueReqId());
			jsonObject.put(DemoAuthCollection.UID_TOKEN, this.getUidToken());
			jsonObject.put(DemoAuthCollection.AADHAAR_NUMBER, aadhaarNum);
			jsonObject.put(DemoAuthCollection.FULL_NAME, this.getFullname());
			jsonObject.put(DemoAuthCollection.GENDER, this.getGender());
			jsonObject.put(DemoAuthCollection.DOB, this.getDob());
			jsonObject.put(DemoAuthCollection.PHONE, this.getPhone());
			jsonObject.put(DemoAuthCollection.PIN, this.getPin());
			jsonObject.put(DemoAuthCollection.TYPE, this.getType());
			jsonObject.put(DemoAuthCollection.AUTH_TYPE, this.getAuthType());
			jsonObject.put(DemoAuthCollection.STATUS, this.isStatus());
			jsonObject.put(DemoAuthCollection.RESPONSE, this.getResponse());
			jsonObject.put(DemoAuthCollection.AVG_RES_TIME, this.getAvgResTime());
			jsonObject.put(DemoAuthCollection.RESIDENTIAL_CONSENT, this.getConsent());
			jsonObject.put(DemoAuthCollection.ERROR_CODE, this.getErrCode());
			jsonObject.put(DemoAuthCollection.ERROR_MSG, this.getErrMsg());
			jsonObject.put(DemoAuthCollection.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;
	}	
}
