package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean(name="allAppActiveUser")
@ViewScoped
public class AllAppActiveUserBean implements Serializable
{
	ArrayList<EmpInfo> list, multiSelectList;
	int total;
	String userId, searchType, selectUserType;
	EmpInfo selectedUser;
	DataBaseMethods obj1=new DataBaseMethods();
	boolean show;
	
	public void searchData()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		userId=obj1.userId();
		list=obj1.allActiveLoginUser(branchId,searchType, selectUserType, conn);
		total=list.size();
		show=true;
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void updateStatus()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		String status =  (String) UIComponent.getCurrentComponent(context).getAttributes().get("serialNo");
		
		int i=obj1.updateLoginStatus(selectedUser.getId(), status);
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Login User "+status+" Sucessfully."));
			searchData();
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
		}
	}
	
	public void sendLoginMessage()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		if(multiSelectList.size()>0)
		{
			String userId=obj1.userId();
			Connection conn=DataBaseConnection.JAVAConnection(); 
			for(EmpInfo ee: multiSelectList)
			{
				String typeMessage="Dear "+ee.getName()+", Your Member No. is "+ee.getUname()+". For More Detail Please Download App From Google Play Store. Use Your Member No. For Your App User Id and Password is "+ee.getPwd()+" and Must Change Your Password After Login. Please Do Not Share Your Password With Anyone. Thanks...";
				obj1.runMessageUrl(branchId,ee.getMobile(), typeMessage, "MESGEE","1207166054344501940", conn);
			}
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(multiSelectList.size()+" Message Send Sucessfully."));
			
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Select Atlease One Contact To Send Message for User's."));
		}
	}
	
	public void sendMsg()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		int s=(int) (Math.random()*999999);
		String msg="Dear "+selectedUser.getName().trim()+", Your App User id is "+selectedUser.getUname()+" & Password is "+s;
		int i=obj1.runMessageUrl(branchId,selectedUser.getMobile(), msg, "system","1207166054328078066",conn);
		if(i>0)
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Password Reset Sucessfully."));
			obj1.updateMemberLoginPassword(selectedUser.getUname(), s);
			searchData();
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error Occured!!!"));
		}
		try {
			conn.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	public ArrayList<EmpInfo> getList() {
		return list;
	}

	public void setList(ArrayList<EmpInfo> list) {
		this.list = list;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public EmpInfo getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(EmpInfo selectedUser) {
		this.selectedUser = selectedUser;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<EmpInfo> getMultiSelectList() {
		return multiSelectList;
	}

	public void setMultiSelectList(ArrayList<EmpInfo> multiSelectList) {
		this.multiSelectList = multiSelectList;
	}

	public String getSelectUserType() {
		return selectUserType;
	}

	public void setSelectUserType(String selectUserType) {
		this.selectUserType = selectUserType;
	}
	
	
	
}
