package mainapp;

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

public class User {
    private int ID;
    private String name;
    private String address;
    private String phone;
    private String afm;
    private String bankAccount;
    private String email;
    private String username;
    private String password;
    private boolean admin;
    
    // Create a User object by passing all required fields
    public User(int ID, String name, String address, String phone, String afm, String bankAccount, 
            String email, String username, String password, boolean isAdmin) {
        this.ID = ID;
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.afm = afm;
        this.bankAccount = bankAccount;
        this.email = email;
        this.username = username;
        this.password = password;
        this.admin = isAdmin;
    }
    
    //create a user object ny quering the DB
    public User(int ID) throws ClassNotFoundException, SQLException, SQLException{
        DB db = DB.getInstance();
        ResultSet rs = DB.selectRows("user", "*", "`id` = "+ID, null, null);
        ArrayList res = new ArrayList<User>();
        while(rs.next()){
            this.name = rs.getString("name");
            this.ID = ID;
            this.address = rs.getString("address");
            this.phone = rs.getString("phone");
            this.afm = rs.getString("afm");
            this.bankAccount = rs.getString("bank_account");
            this.email = rs.getString("email");
            this.username = rs.getString("username");
            this.password = rs.getString("password");
            this.admin = (rs.getInt("is_admin") == 1);
        }
    }
    
    public static ArrayList<User> getAllUsers() throws SQLException, ClassNotFoundException{
        DB db = DB.getInstance();
        ResultSet rs = DB.selectRows("user", "*", null, "`is_admin` DESC, `name` ASC", null);
        ArrayList res = new ArrayList<User>();
        while(rs.next()){
            res.add(new User(rs.getInt("id"), rs.getString("name"), rs.getString("address"), 
                    rs.getString("phone"), rs.getString("afm"), rs.getString("bank_account"), 
                    rs.getString("email"), rs.getString("username"), rs.getString("password"), 
                    rs.getInt("is_admin") == 1));
        }
        return res;
    }
    
    public static void delete(int ID) throws ClassNotFoundException, SQLException {
        DB db = DB.getInstance();
        DB.deleteRow("user", "`id` = "+ID);
    }
    
    public static ArrayList search(String term) throws ClassNotFoundException, SQLException {
        String where = "`name` LIKE '%"+term+"%' OR `afm` LIKE '%"+term+"%'";
        ResultSet rs = DB.selectRows("user", "*", where, "`is_admin` DESC, `name` ASC", null);
        ArrayList res = new ArrayList<User>();
        while(rs.next()){
            res.add(new User(rs.getInt("id"), rs.getString("name"), rs.getString("address"), 
                    rs.getString("phone"), rs.getString("afm"), rs.getString("bank_account"), 
                    rs.getString("email"), rs.getString("username"), rs.getString("password"), 
                    rs.getInt("is_admin") == 1));
        }
        return res;
    }

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

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

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

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

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

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

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

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

    /**
     * @return the amf
     */
    public String getAfm() {
        return afm;
    }

    /**
     * @param afm the amf to set
     */
    public void setAfm(String afm) {
        this.afm = afm;
    }

    /**
     * @return the bankAccount
     */
    public String getBankAccount() {
        return bankAccount;
    }

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

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

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

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

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

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

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

    /**
     * @return the admin
     */
    public boolean isAdmin() {
        return admin;
    }

    /**
     * @param admin the admin to set
     */
    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
}
