package other_package;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;

import org.primefaces.model.UploadedFile;



@ManagedBean(name="manageProblem")
@ViewScoped

public class ManageProblemBean implements Serializable
{
	UploadedFile image1;
	String image1Path, problemType, problemDesc, userId, type, compNo, typeMessage;
	Date dt=new Date();
	ArrayList<ProblemInfo> list;
	ProblemInfo selected;
	String status, remark, id;
	DataBaseMethods obj=new DataBaseMethods();
	
	public ManageProblemBean() 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		userId=obj.userId();
		complainedNo();
		
		Connection conn=DataBaseConnection.JAVAConnection();
		list=obj.viewProblem(branchId,conn);
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public String addProb()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection con= DataBaseConnection.JAVAConnection();
	
		userId=obj.userId();
		String pDate=new SimpleDateFormat("dd_MM_yyyy").format(dt);
		if (image1 != null && image1.getSize() > 0) 
		{
			
			String img1= image1.getFileName();
			String arr1[]=img1.split("\\.");
			image1Path=compNo+"_"+pDate+"."+arr1[1];
			obj.makeProfile(image1,image1Path);
		}
		
		complainedNo();
		
		int i=obj.addProblem(branchId,dt, problemType, problemDesc, image1Path, "pending", compNo, userId,con);
		if(i>=1)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Problem Added Sucessfully"));
			sendMessage("add");
			
			compNo=problemType=problemDesc=typeMessage="";
			image1=null;userId="";
			
			complainedNo();
			
			return "manageProblem.xhtml";
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured"));
		}
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}
	
	public void updateDetail()
	{
		id=selected.getProblemId();
	}
	
	public void updateProblem()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		status="solved";
		
		int i=obj.updateProblemStatus(status, remark, id, conn);
		if(i>=1)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Problem Resolved Sucessfully"));
			sendMessage("update");
			list=obj.viewProblem(branchId,conn);
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!! Please check"));
		}
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	

	public void complainedNo()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection con= DataBaseConnection.JAVAConnection();
		int cmpNo=Integer.parseInt(obj.complainedNo(branchId,con));
		if(cmpNo>=1 && cmpNo<=9)
		{
			compNo="000"+cmpNo;
		}
		else if(cmpNo>=10 && cmpNo<=99)
		{
			compNo="00"+cmpNo;
		}
		else if(cmpNo<=100 && cmpNo<=999)
		{
			compNo="0"+cmpNo;
		}
		else
		{
			compNo=String.valueOf(cmpNo);
		}
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void sendMessage(String msgModule) 
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection con= DataBaseConnection.JAVAConnection();
		
		String contactNo=obj.contactNoByUserId(userId, con);
		if(msgModule.equalsIgnoreCase("add"))
		{
			typeMessage="Complained No. COMP"+compNo  +" is Sucessfully "+ " Registered By - "+userId+" .Your Complained will be resolved asap.";
			obj.runMessageUrlForClientProblemModule(branchId,"9982311326", typeMessage, "System",con);
		}
		else
		{
			typeMessage="Complained No. COMP"+selected.getCompNo()  +" is Resolved Sucessfully. For more information log on Your ERP Portal "  ;
		}
			
		obj.runMessageUrlForClientProblemModule(branchId,contactNo, typeMessage, userId,con);
		
		
		try {
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}


	public UploadedFile getImage1() {
		return image1;
	}


	public void setImage1(UploadedFile image1) {
		this.image1 = image1;
	}


	public String getImage1Path() {
		return image1Path;
	}


	public void setImage1Path(String image1Path) {
		this.image1Path = image1Path;
	}


	public String getProblemType() {
		return problemType;
	}


	public void setProblemType(String problemType) {
		this.problemType = problemType;
	}


	public String getProblemDesc() {
		return problemDesc;
	}


	public void setProblemDesc(String problemDesc) {
		this.problemDesc = problemDesc;
	}


	public String getUserId() {
		return userId;
	}


	public void setUserId(String userId) {
		this.userId = userId;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getCompNo() {
		return compNo;
	}


	public void setCompNo(String compNo) {
		this.compNo = compNo;
	}


	public Date getDt() {
		return dt;
	}


	public void setDt(Date dt) {
		this.dt = dt;
	}


	
	
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}


	public ArrayList<ProblemInfo> getList() {
		return list;
	}


	public void setList(ArrayList<ProblemInfo> list) {
		this.list = list;
	}


	public ProblemInfo getSelected() {
		return selected;
	}


	public void setSelected(ProblemInfo selected) {
		this.selected = selected;
	}
}

