package mainapp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class Factory {
    private int ID;
    private String name;
    private String address;
    private String owner;
    private String phone;
    
    public Factory(int ID, String name, String address, String owner, String phone) {
        this.ID = ID;
        this.name = name;
        this.address = address;
        this.owner = owner;
        this.phone = phone;
    }
    
    public Factory(int ID) throws ClassNotFoundException, SQLException {
        DB db = DB.getInstance();
        ResultSet rs = DB.selectRows("factory", "*", "`id` = "+ID, null, null);
        ArrayList res = new ArrayList<>();
        while(rs.next()){
            this.ID = ID;
            this.address = rs.getString("address");
            this.phone = rs.getString("phone");
            this.name = rs.getString("name");
            this.owner = rs.getString("owner");
        }
    }
    
    public ArrayList<Production> getAllProductions() throws ClassNotFoundException, SQLException {
        ResultSet rs = DB.selectRows("production", "*", "`factory_id` = "+this.getID(), null, null);
        ArrayList res = new ArrayList<>();
        while(rs.next()){
            res.add(new Production(rs.getInt("id"), rs.getInt("producer_id"), 
                    rs.getInt("factory_id"), rs.getString("timestamp"), 
                    rs.getFloat("olives_weight"), rs.getFloat("oil_weight"),
                    rs.getFloat("acidity"), (rs.getInt("is_bio") == 1), rs.getFloat("costs"),
                    (rs.getInt("sold") == 1), rs.getFloat("sold_price")));
        }
        return res;
    }
    
    public HashMap getAllProductionsByUser() throws SQLException, ClassNotFoundException{
        String sql = "SELECT `producer_id`, SUM(`sold_price`) as `total` "+
                "FROM `production` WHERE `factory_id` = "+this.getID()+" AND `sold` = 1 GROUP BY `producer_id`";
        ResultSet rs = DB.execSelectSql(sql);
        HashMap map = new HashMap();
        while(rs.next()){
            map.put(rs.getInt("producer_id"), rs.getFloat("total"));
        }
        return map;
    }
    
    public static ArrayList<Factory> getAllFactories() throws SQLException, ClassNotFoundException{
        DB db = DB.getInstance();
        ResultSet rs = DB.selectRows("factory", "*", null, "`name` ASC", null);
        ArrayList res = new ArrayList<>();
        while(rs.next()){
            res.add(new Factory(rs.getInt("id"), rs.getString("name"), rs.getString("address"), 
                    rs.getString("owner"), rs.getString("phone")));
        }
        return res;
    }
    
    public static void delete(int ID) throws ClassNotFoundException, SQLException {
        DB db = DB.getInstance();
        DB.deleteRow("factory", "`id` = "+ID);
    }

    /**
     * @return the ID
     */
    public int getID() {
        return ID;
    }

    /**
     * @param ID the ID to set
     */
    public void setID(int ID) {
        this.ID = ID;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the owner
     */
    public String getOwner() {
        return owner;
    }

    /**
     * @param owner the owner to set
     */
    public void setOwner(String owner) {
        this.owner = owner;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
}
