package fwauth;
import javax.swing.*;

import org.json.simple.JSONObject;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Log extends JFrame {

public static void main(String[] args) throws IOException {
Log frameTabel = new Log();
}

	JButton blogin = new JButton("Login");
	JPanel panel = new JPanel();
	JTextField txuser = new JTextField(15);
	JPasswordField pass = new JPasswordField(15);
	 JLabel l1, l2;

Log() throws IOException{
	super("Floating Walls Authentication");
	setSize(450,300);
	setLocation(500,280);
	panel.setLayout (null);
	
	l1 = new JLabel("Username");
	l2 = new JLabel("Password");
	JLabel myLabel = new JLabel();
	myLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/logo.png")));

	myLabel.setBounds(120, 10, 400, 40);
	l1.setBounds(50, 70, 400, 30);
	txuser.setBounds(120,75,150,20);
	
	l2.setBounds(50, 110, 200, 30);
	pass.setBounds(120,115,150,20);
	
	blogin.setBounds(120,155,80,20);
	
	
	panel.add(myLabel);
	
	panel.add(blogin);
	panel.add(l1);
	panel.add(l2);
	panel.add(txuser);
	panel.add(pass);
	
	getContentPane().add(panel);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	panel.getRootPane().setDefaultButton(blogin);

	setVisible(true);
	actionlogin();
	
	

}

public void setUnameAndPass() throws IOException
{
	Properties prop = new Properties();
	InputStream input = null;
	String propertyFilePath =System.getProperty("user.dir")+"/config.properties";
	try {
		input = new FileInputStream(new File(propertyFilePath));
		prop.load(input);
		String credentials = prop.getProperty("credentials").trim();
		
		if(!credentials.isEmpty() && credentials!=null && !credentials.equals(""))
		{
			txuser.setText(credentials.split("-")[0]);
			pass.setText(credentials.split("-")[1]);
		}
	} catch (Exception e) {
		
	}
	
}
public void actionlogin() throws IOException{
	setUnameAndPass();
	blogin.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent ae) {
			String puname = txuser.getText();
			String ppaswd = pass.getText();
			if(puname.isEmpty())
			{
				JOptionPane.showMessageDialog(null,"Please enter Username");
				return;
			}
			if(ppaswd.isEmpty())
			{
				JOptionPane.showMessageDialog(null,"Please enter Password");
				return;
			}
			
			String macId=MAC.GetMACAddressofClient();
			
			try {
				JSONObject returnObj=HttpCallURL.sendPOST(puname,ppaswd,macId);
				String errorCode=returnObj.get("errorCode").toString();
				/*Properties prop;
				String propertyFilePath;
				prop = new Properties();
				propertyFilePath = System.getProperty("user.dir")+"/config.properties";
				try {
					
					InputStream input = null;
					
					input = new FileInputStream(new File(propertyFilePath));
					prop.load(input);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					
				}*/
    			
  
				if(errorCode.equals("0"))
				{
					try {
						/*prop.setProperty("credentials",puname+"-"+ppaswd);
						File file = new File(propertyFilePath);
						FileOutputStream fileOut = new FileOutputStream(file);
						prop.store(fileOut, "");
						fileOut.close();*/
						
						String jarSessionId=returnObj.get("jar_session_id").toString();
						if(!jarSessionId.isEmpty())
						{
							//String load_main_page_url="http://demo.tarka.in/floating_walls/index_jar.html?jar_session_id=";
							String load_main_page_url="http://aws.tarka.in/floating_walls/index_jar.html?jar_session_id=";
							OpenBrowser.OpenBrowserAndCallURL(load_main_page_url+jarSessionId);
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						
					}
					System.exit(0);
				}
				else
				{
					JOptionPane.showMessageDialog(null,returnObj.get("msg").toString());
					
				}
				System.out.println("errorCode....."+errorCode);
			} catch (IOException e) {
				System.out.println(e.getMessage());
				// TODO Auto-generated catch block
				JOptionPane.showMessageDialog(null,e.getMessage()+" Connection Error ");
			}
			
			
		
		}
	});
}
}