package metab;
import java.io.*;

public class TDSubject implements Serializable{

	public static final int _LIGHT = 1;
	public static final int _MODERATE = 1 << 1;
	public static final int _HEAVY = 1 << 2;
	public static final int _BEER = 1 << 3;
	public static final int _WINE = 1 << 4;
	public static final int _HARD_LIQUOR = 1 << 5;
	public static final int _METRIC = 1 << 6;
	public static final int _IMPERIAL = 1 << 7;
	public static final int _ERROR = 1 << 8;
	private int bitFlag, age;
	private double numberOfDrinks, hoursDrinking, weight;
	private char gender;
	private String subjectName;


	TDSubject(int age, char gender, double numberOfDrinks, double hoursDrinking,
				String subjectName, double weight, int bitFlag){

		this.age = age;
		this.gender = gender;
		this.numberOfDrinks = numberOfDrinks;
		this.hoursDrinking = hoursDrinking;
		this.subjectName = subjectName;
		this.bitFlag = bitFlag;
		if ((bitFlag & _METRIC) != 0)
			this.weight = weight;
		else if ((bitFlag & _IMPERIAL) != 0)
			this.weight = weight / 2.20462262; // lb to kg

	}

	public int getAge(){

		return age;

	}

	public double getBAC(){

		double oz = 0, BAC, hourlyDecrease = 0;
        double TBW = ((gender == 'M')?(weight * .58):(weight * .49)) * 1000;
				//Total Body Water in milliliters
        double gramsIn100mlBlood = (23.36 / TBW) * .806 * 100;
				//Grams alcohol per one hundred milliliters blood
		if ((bitFlag & _BEER) != 0)
            oz = numberOfDrinks * 12 * 0.06;
        else if ((bitFlag & _WINE) != 0)
            oz = numberOfDrinks * 10 * 0.10;
        else if ((bitFlag & _HARD_LIQUOR) != 0)
            oz = numberOfDrinks * 1 * 0.48;

        //Actual amount of ethanol consumed in ounces
        if ((bitFlag & _LIGHT) != 0)
        	hourlyDecrease = .012;
        else if ((bitFlag & _MODERATE) != 0)
        	hourlyDecrease = .017;
        else if ((bitFlag & _HEAVY) != 0)
        	hourlyDecrease = .020;
        //Hourly decrease in BAC
		BAC = oz * gramsIn100mlBlood;//Instantaneous BAC
        BAC = BAC - (hoursDrinking * hourlyDecrease);//Final BAC for the subject
        return BAC;

    }

	public int getDrinkingHistory(){

		if ((bitFlag & _LIGHT) != 0)
			return _LIGHT;
		else if ((bitFlag & _MODERATE) != 0)
			return _MODERATE;
		else if ((bitFlag & _HEAVY) != 0)
			return _HEAVY;
		else
			return _ERROR;

    }

    public char getGender(){

    	return gender;

    }

    public double getNumberOfDrinks(){

		return numberOfDrinks;

    }

	public double getHoursDrinking(){

		return hoursDrinking;

	}

	public String getSubjectName(){

		return subjectName;

	}

	public int getTypeOfDrink(){

		if ((bitFlag & _BEER) != 0)
			return _BEER;
		else if ((bitFlag & _WINE) != 0)
			return _WINE;
		else if ((bitFlag & _HARD_LIQUOR) != 0)
			return _HARD_LIQUOR;
		else
			return _ERROR;

	}

	public double getWeight(){

		if ((bitFlag & _IMPERIAL) != 0){
            return weight * 2.20462262;
        }
        else {
            return weight;
        }

	}

}