package main_package;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
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.context.FacesContext;
import javax.servlet.http.HttpSession;

@ManagedBean(name="sendDLTMessage")
@ViewScoped

public class SendDLTMessageBean implements Serializable
{
	boolean showList = false, other = false, show = false;
	String selectType, typeMessage;
	String contactList;
	String selectLanguage;
	boolean showHindi = false, showEnglish = true;
	ArrayList<LoanInfo> pList, multipleRows;
	String userId;
	DataBaseMethods db = new DataBaseMethods();
	int branchId;
	
	
	
	/*public void checkLanguage()
	{
		// System.out.println(selectLanguage);
		if(selectLanguage.equalsIgnoreCase("english"))
		{
			showEnglish=true;showHindi=false;
		}
		else
		{
			showEnglish=false;showHindi=true;
			RequestContext.getCurrentInstance().execute("PF('dlg').show();");
		}
	}*/
	
	
	
	public SendDLTMessageBean() 
	{
		userId=db.userId();
	}
	
	
	
	public void checkList()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		 branchId = (int) ss.getAttribute("branchId");
		show=true; 
		
		if(selectType.equals("-1"))
		{
			other=true;
			showList=false;
		}
		else
		{
			other=false;
			showList=true;
			if(selectType.equalsIgnoreCase("personalloan"))
			{
				pList = db.personalLoanList(branchId,"All", "unpaid","-1","");
			}
			else if(selectType.equalsIgnoreCase("vechicleloan"))
			{
				pList = db.vehicleLoanList(branchId,"All");
			}
			else if(selectType.equalsIgnoreCase("propertyloan"))
			{
				pList = db.propertyLoanList(branchId,"All");
			}
			else if(selectType.equalsIgnoreCase("grouploan"))
			{
				pList = db.groupLoanList(branchId,"All");
			}
			else if(selectType.equalsIgnoreCase("-2"))
			{
				pList = db.shareHolderListForMsg(branchId);
			}
		}
	}
	
	public String send()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		 branchId = (int) ss.getAttribute("branchId");
		if(multipleRows.size()>0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			String check=db.checkStatusInMessageSettingTable("Message Module");
			if(check.equals("active"))
			{
				for(LoanInfo ll: multipleRows)
				{
					db.runMessageUrl(branchId,ll.getContactNo(), typeMessage, userId,"pending", conn);
				}
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(multipleRows.size()+" Message Send Sucessfully"));
			}
			else
			{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Message For This Module is not Active. Please Go to In User Block and Active Message For This Module."));
			}
			
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please Select Atleast One Contact no from List."));
			return "";
		}
		return "sendMessage.xhtml";
	}
	
	public void sendWhatsApp()
	{
		if(multipleRows.size()>0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			for(LoanInfo ll: multipleRows)
			{
				//db.runWhatsAppUrl(ll.getContactNo(), typeMessage, conn);
				db.runWhatsAppDirect(ll.getContactNo(), typeMessage, conn);
			}
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(multipleRows.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 Atleast One Contact no from List."));
		}
	}
	
	public String sendOtherMsg()
	{
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		 branchId = (int) ss.getAttribute("branchId");
		String contactNo = null;boolean check=false;
		String cList[]=contactList.split(",");
		/*try {
			typeMessage=URLEncoder.encode(typeMessage,"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		
		for(int i=0;i<cList.length;i++)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			
			if(cList[i].trim().length()==10)
			{
				contactNo=cList[i].trim();
				check=true;
			}
			
			if(check==true)
			{
				String checkStatus=db.checkStatusInMessageSettingTable("Message Module");
				if(checkStatus.equals("active"))
				{
					db.runMessageUrl(branchId,contactNo, typeMessage, userId,"pending", conn);
				}
				else
				{
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Message For This Module is not Active. Please Go to In User Block and Active Message For This Module."));
					return "";
				}
				check=false;
			}
			
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Message Sent Sucessfully"));
		
		return "sendMessage.xhtml";
	}
	
	
	
	/*public void checkBalanceHindi()
	{
		 String output  =getUrlContents("http://199.189.250.157/smsclient//balancecheckapi.php?username=pankaj-alwar&&password=84006470");
		 FacesContext fc=FacesContext.getCurrentInstance();
		 fc.addMessage(null, new FacesMessage("You have "+output+" message left "));
		 
	}*/
	
	public void checkBalanceEnglish()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
			
			String balanceApi=db.checkSMessageApiDetailFromTable("balance_url","",conn);;
		 
			String output  =getUrlContents(balanceApi);
		 FacesContext fc=FacesContext.getCurrentInstance();
		 fc.addMessage(null, new FacesMessage("You have "+output+" message left "));
		 
		 try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	private static String getUrlContents(String theUrl)
	  {
	    StringBuilder content = new StringBuilder();
	    try
	    {

	      URL url = new URL(theUrl);
	      URLConnection urlConnection = url.openConnection();
	      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
	 
	      String line;
	      while ((line = bufferedReader.readLine()) != null)
	      {
	        content.append(line + "\n");
	      }
	      bufferedReader.close();
	    }
	    catch(Exception e)
	    {
	      e.printStackTrace();
	    }
	    return content.toString();
	  }

	

	public boolean isShowList() {
		return showList;
	}

	public void setShowList(boolean showList) {
		this.showList = showList;
	}

	public ArrayList<LoanInfo> getpList() {
		return pList;
	}

	public void setpList(ArrayList<LoanInfo> pList) {
		this.pList = pList;
	}

	public String getSelectType() {
		return selectType;
	}

	public void setSelectType(String selectType) {
		this.selectType = selectType;
	}

	public String getTypeMessage() {
		return typeMessage;
	}

	public void setTypeMessage(String typeMessage) {
		this.typeMessage = typeMessage;
	}

	


	public boolean isOther() {
		return other;
	}



	public void setOther(boolean other) {
		this.other = other;
	}



	public String getContactList() {
		return contactList;
	}



	public void setContactList(String contactList) {
		this.contactList = contactList;
	}



	public String getSelectLanguage() {
		return selectLanguage;
	}



	public void setSelectLanguage(String selectLanguage) {
		this.selectLanguage = selectLanguage;
	}



	public boolean isShowHindi() {
		return showHindi;
	}



	public void setShowHindi(boolean showHindi) {
		this.showHindi = showHindi;
	}



	public boolean isShowEnglish() {
		return showEnglish;
	}



	public void setShowEnglish(boolean showEnglish) {
		this.showEnglish = showEnglish;
	}

	public ArrayList<LoanInfo> getMultipleRows() {
		return multipleRows;
	}

	public void setMultipleRows(ArrayList<LoanInfo> multipleRows) {
		this.multipleRows = multipleRows;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}
	
	
}