package mainapp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PricingOptions {
    private float normalPrice;
    private float bioPrice;
    private float acidityStep;
    
    public PricingOptions () throws ClassNotFoundException, SQLException {
        DB db = DB.getInstance();
        ResultSet rs = DB.selectRows("pricing", "*", null, null, null);
        this.normalPrice = 0;
        this.bioPrice = 0;
        this.acidityStep = 0;
        while(rs.next()){
            if (rs.getString("type").equals("normal_price")) {
                this.normalPrice = rs.getFloat("value");
            }
            else if (rs.getString("type").equals("bio_price")) {
                this.bioPrice = rs.getFloat("value");
            }
            else if (rs.getString("type").equals("acidity_step")) {
                this.acidityStep = rs.getFloat("value");
            }
        }
    }
    
    public PricingOptions (boolean loadPrices) throws ClassNotFoundException, SQLException {
        if (loadPrices == true) {
            DB db = DB.getInstance();
            ResultSet rs = DB.selectRows("pricing", "*", null, null, null);
            this.normalPrice = 0;
            this.bioPrice = 0;
            this.acidityStep = 0;
            while(rs.next()){
                if (rs.getString("type").equals("normal_price")) {
                    this.normalPrice = rs.getFloat("value");
                }
                else if (rs.getString("type").equals("bio_price")) {
                    this.bioPrice = rs.getFloat("value");
                }
                else if (rs.getString("type").equals("acidity_step")) {
                    this.acidityStep = rs.getFloat("value");
                }
            }
        }
        else {
            this.normalPrice = 0;
            this.bioPrice = 0;
            this.acidityStep = 0;
        }
    }
    
    public boolean save() {
        try {
            DB.execUpdateSql("UPDATE `pricing` SET `value` = "+this.normalPrice+" WHERE `type` = 'normal_price'");
            DB.execUpdateSql("UPDATE `pricing` SET `value` = "+this.bioPrice+" WHERE `type` = 'bio_price'");
            DB.execUpdateSql("UPDATE `pricing` SET `value` = "+this.acidityStep+" WHERE `type` = 'acidity_step'");
        } catch (ClassNotFoundException | SQLException ex) {
            return false;
        }
        return true;
    }

    /**
     * @return the normalPrice
     */
    public float getNormalPrice() {
        return normalPrice;
    }

    /**
     * @param normalPrice the normalPrice to set
     */
    public void setNormalPrice(float normalPrice) {
        this.normalPrice = normalPrice;
    }

    /**
     * @return the bioPrice
     */
    public float getBioPrice() {
        return bioPrice;
    }

    /**
     * @param bioPrice the bioPrice to set
     */
    public void setBioPrice(float bioPrice) {
        this.bioPrice = bioPrice;
    }

    /**
     * @return the acidityStep
     */
    public float getAcidityStep() {
        return acidityStep;
    }

    /**
     * @param acidityStep the acidityStep to set
     */
    public void setAcidityStep(float acidityStep) {
        this.acidityStep = acidityStep;
    }
}
