package in.gov.digilocker.uidai.services.xmlresponse;

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.springframework.stereotype.Component;

@XmlRootElement(name = "AuthRes")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "code", "err", "info", "ret", "ts", "txn", "xmlResp", "msg" })
@Component
public class AuthResponse {
	@XmlAttribute(name= "code") private String code;
	@XmlAttribute(name= "err")	private String err;	
	@XmlAttribute(name= "info")	private String info;
	@XmlAttribute(name= "ret") private String ret;
	@XmlAttribute(name= "ts") private String ts;
	@XmlAttribute(name= "txn")private String txn;
	@XmlElement(name= "xmlResp")private String xmlResp;
	@XmlElement(name= "msg")	private String msg;

	public AuthResponse() {
		this.code = "";
		this.err = "";
		this.info = "";
		this.ret = "";
		this.ts = "";
		this.txn = "";
		this.xmlResp = "";
		this.msg= "";
	}

	public String getCode() {
		return this.code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getErr() {
		return this.err;
	}

	public void setErr(String err) {
		this.err = err;
	}

	public String getInfo() {
		return this.info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getRet() {
		return this.ret;
	}

	public void setRet(String ret) {
		this.ret = ret;
	}

	public String getTs() {
		return this.ts;
	}

	public void setTs(String ts) {
		this.ts = ts;
	}

	public String getTxn() {
		return this.txn;
	}

	public void setTxn(String txn) {
		this.txn = txn;
	}

	public String getXmlResp() {
		return this.xmlResp;
	}

	public void setXmlResp(String xmlResp) {
		this.xmlResp = xmlResp;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public static AuthResponse buildAuthResponse(String respXML) throws JAXBException {
		JAXBContext context = JAXBContext.newInstance(new Class[] { AuthResponse.class });
		Unmarshaller um = context.createUnmarshaller();
		StringReader reader = new StringReader(respXML);
		AuthResponse authResponse= new AuthResponse();
		authResponse = (AuthResponse) um.unmarshal(reader);
		return authResponse;
	}
}
