package com.example.schedulerapp;

import java.net.*;
import java.io.*;
import java.util.ArrayList;

public class ScheduleClient {
    private PrintWriter writer;
    protected ArrayList<String> shifts;
    protected ArrayList<String> employees;

    public ScheduleClient(String hostname, int port) {
        this.shifts =  new ArrayList<>();
        this.employees = new ArrayList<>();
        try {
            Socket socket = new Socket(hostname, port);
            System.out.println("Connected to the scheduling server");

            new ReceiveThread(socket, this).start();

            OutputStream output = socket.getOutputStream();
            this.writer = new PrintWriter(output, true);

        } catch (UnknownHostException error) {
            System.out.println("Server not found: " + error.getMessage());
        } catch (IOException error) {
            System.out.println("I/O Error: " + error.getMessage());
        }
    }

    void getAllShifts(){
        writer.println("allEmployees");
    }

    void getAllEmployees(){
        writer.println("allShifts");
    }

    void addEmployee(String first_name, String last_name) {
        writer.println("addEmployee/" + first_name + "/" + last_name);
    }

    void removeEmployee(String first_name, String last_name) {
        writer.println("removeEmployee/" + first_name + "/" + last_name);
    }

    void printAllEmployees(){
        System.out.println(this.employees);
    }

    String getEmployeeByID(int id) {
        if (!this.employees.isEmpty()) {
            for(String e: this.employees){
                if(!e.equals("allEmployees")){
                    String[] eSplit = e.split("\\.");
                    try {
                        if(Integer.parseInt(eSplit[0]) == id){
                            return eSplit[1] + " " + eSplit[2];
                        }
                    } catch (NumberFormatException exception){
                        System.out.println("getEmployeeByID: string cannot be integer parsed");
                    }

                }
            }
            return "No Employee with that ID";
        }
        return "No Employees";
    }

    void logOut() {
        writer.println("logout");
    }

    public static void main(String[] args) {
        ScheduleClient client = new ScheduleClient("localhost", 8989);
        while(client.employees.isEmpty()){
            System.out.println("Retrieving Data...");
        }
        System.out.println("Data Retrieved!");

        /*
        //Testing stuff that should be cleaned up for the alpha.

        client.printAllEmployees();
        System.out.println(client.getEmployeeByID(3));
        System.out.println(client.getEmployeeByID(7));
        client.printAllEmployees();

         */
    }
}