/*
    Point of Sale System Project
    Authors: Clayton Barber, Brandon Barton, Declan Brennan, Maximilian Hasselbusch, Eric Metcalf
    Last Updated: 20 November 2015
 */
package pos;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static pos.PointofSale.system;

public class Sys {

    boolean loggedIn;
    String error;
    Integer eid;
    String name;
    boolean isManager;
    Sale s;
    Connection con;
    private PrintStream ps;

    Sys(Connection con) {
        loggedIn = false;
        s = null;
        error = "No error";
        this.con = con;
    }

    public int login(String username, String password) {
        try {
            Statement smt = con.createStatement();
            String query = "select * from pos.employees where username = '" + username
                    + "' and password = '" + password + "'";
            ResultSet res = smt.executeQuery(query);
            if (res.next()) {
                name = res.getString("name") + " " + res.getString("surname");
                eid = res.getInt("eid");
                byte isLoggedIn = res.getByte("islogin");
                byte isMan = res.getByte("manPriv");
                if(isLoggedIn == 1){
                    return 3; //where 3 means already logged in
                }
                else smt.executeUpdate("update pos.employees set islogin=1 where eid ='" + eid + "'");
                if (isMan == 1) {
                    loggedIn = true;
                    isManager = true;
                    return 2;
                }
                else{
                    loggedIn = true;
                    isManager = false;
                    return 1;
                }
            } else {
                return 0;
            }
        } catch (SQLException sqe) {
            System.err.println("Unable to find requested entries");
            return 0;
        }
    }

    public void logout() {
        try {
            Statement st = con.createStatement();
            st.executeUpdate("update pos.employees set islogin = 0 where eid = '" + eid + "'");
        } catch (SQLException se) {
            System.err.println("Unable to log user out of system.");
        }
        LoginNew l = new LoginNew(this, con);
    }

    public void buildSale() {
        s = new Sale(con, ps);
    }

    public String getError() {
        return error;
    }

    public void addEmployee() {
        Employee e = new Employee(con, this);
        e.addEmployee();
    }

    void removeEmployee() {
        Employee e = new Employee(con, this);
        e.removeEmployee();
    }

    void editManPrivilege() {
        Employee e = new Employee(con, this);
        e.editManPrivilege();
    }

    void changePassword() {
        Employee e = new Employee(con, this);
        e.changePassword();
    }

    void addItem() {
        AdministrativeFunctions af = new AdministrativeFunctions(con);
        af.addItem();
    }

    void InventoryLevels() {
        AdministrativeFunctions af = new AdministrativeFunctions(con);
        af.InventoryLevels();
    }

    void removeItem() {
        AdministrativeFunctions af = new AdministrativeFunctions(con);
        af.removeItem();
    }

    void editPrice() {
        AdministrativeFunctions af = new AdministrativeFunctions(con);
        af.updatePrice();
    }

    void viewSales() {
        SalesFunctions sf = new SalesFunctions(con);
        sf.viewSales();
    }

    void voidTransaction() {
        SalesFunctions sf = new SalesFunctions(con);
        sf.voidTransaction();
    }

    void updateDatabase() {
        File file = new File("towrite.txt");
        if (file.exists()) {
            try {
                FileInputStream fs = new FileInputStream(file);
                BufferedReader br = new BufferedReader(new InputStreamReader(fs));
                while (br.ready()) {
                    String line = br.readLine();
                    //System.err.println(line);
                    Statement us = con.createStatement();
                    us.executeUpdate(line);
                }
                br.close();
                file.delete();
            } catch (FileNotFoundException ex) {
                System.err.println("Unable to find file");
            } catch (IOException ex) {
                System.err.println(ex.getMessage());
            } catch (SQLException ex) {
                System.err.println("SQL Problems: " + ex.getMessage());
            }
        }
        try {
            Statement q = con.createStatement();
            ResultSet result = q.executeQuery("select mtid from dual");
            result.next();
            File f = new File("peripherals.txt");
            if (!f.exists()) {
                f.createNewFile();
            }
            FileWriter fw = new FileWriter(f);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(result.getNString(1));
            bw.newLine();
            bw.close();
        } catch (SQLException sqe) {
            System.err.println("SQL BADNESS");
        } catch (IOException ex) {
            System.err.println("I/O BADNESS");
        }
    }
}

@@ -2246,24 +2246,1021 @@
  logout() %7B%0A
+        // saves games sku and price to text file%0A        try %7B%0A            Statement st = con.createStatement();%0A            ResultSet result = st.executeQuery(%22select sku, price from pos.games order by sku%22);%0A            File file = new File(%22games.txt%22);%0A            try %7B%0A                String sku;%0A                FileWriter writer = new FileWriter(file);%0A                BufferedWriter bwriter = new BufferedWriter(writer);%0A                while (result.next()) %7B%0A                    //result.next();%0A                    sku = String.format(%22%2508d%22, result.getInt(1));%0A                    bwriter.write(sku + %22=%22 + result.getDouble(2));%0A                    bwriter.newLine();%0A                %7D%0A                bwriter.close();%0A            %7D catch (IOException e) %7B%0A                System.err.println(%22File input error: %22 + e.getMessage());%0A            %7D%0A        %7D catch (SQLException e)    %7B%0A            System.err.println(%22Unable to create database text file.%22 + e.getMessage());%0A        %7D%0A
         try 

save games sku and price to text file upon logout