package WifiServer;

import DBase.DBAdm;
import Interfaz.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * @author Jose
 */
public class ServeClient extends Thread {

    private Socket incoming;
    PrintWriter out;
    BufferedReader in;
    MainPanelWifi mpw;

    public ServeClient(Socket s,MainPanelWifi mpw) {
        incoming = s;
        this.mpw=mpw;
    }

    @Override
    public void run() {

        try {
            

            
            in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
            out = new PrintWriter(incoming.getOutputStream(), true);

            String input = in.readLine();
            String[] inputArray = input.split(";");
            String ip = inputArray[1];
            mpw.defaultListModel.addElement("Equipo :"+ip);
            mpw.textArea.append("Se ha conectado : "+ip+"\n\n");
            int i = mpw.defaultListModel.getSize()-1;

            String query = inputArray[0]+";";
            DBAdm DB = new DBAdm();
            DB.openDb();
            boolean authenticated = DB.obtenerUsuario(query);
            DB.closeDb();
            out.println(authenticated);
            /*
            out.println("Hello! Enter BYE to exit.");

            boolean done = false;
            while (!done) {
            String str = in.readLine();
            if (str == null) {
            done = true;
            } else {
            out.println("Echo " + str);

            if (str.trim().equals("BYE")) {
            done = true;
            }
            }
            }*/
            mpw.textArea.append("Se ha desconectado : "+ip+"\n\n");
            incoming.close();
            mpw.defaultListModel.remove(i);
        } catch (Exception e) {
            e.printStackTrace();

        }

    }
}
