package com.smentkowski.apps.utilities.planager.objects;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by aarons on 2/28/15.
 */
public class LegacyAttendance implements Parcelable, Comparable {

    private long mDateOfAttendance;
    private int mAmountAdded;
    private int mTotalAmountLeft;

    public LegacyAttendance() {
    }

    public LegacyAttendance(Parcel in) {
        mDateOfAttendance = in.readLong();
        mAmountAdded = in.readInt();
        mTotalAmountLeft = in.readInt();
    }

    public LegacyAttendance(long attendedDate, int amountAdded, int totalAmountLeft) {
        mDateOfAttendance = attendedDate;
        mAmountAdded = amountAdded;
        mTotalAmountLeft = totalAmountLeft;
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public LegacyAttendance createFromParcel(Parcel in) {
            return new LegacyAttendance(in);
        }
        public LegacyAttendance[] newArray(int size) {
            return new LegacyAttendance[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(mDateOfAttendance);
        dest.writeInt(mAmountAdded);
        dest.writeInt(mTotalAmountLeft);
    }

    public int getAmountAdded() {
        return mAmountAdded;
    }

    public void setAmountAdded(int amountAdded) {
        this.mAmountAdded = amountAdded;
    }

    public long getDateOfAttendance() {
        return mDateOfAttendance;
    }

    public void setDateOfAttendance(long dateOfAttendance) {
        this.mDateOfAttendance = dateOfAttendance;
    }

    public int getTotalAmountLeft() {
        return mTotalAmountLeft;
    }

    public void setTotalAmountLeft(int totalAmountLeft) {
        this.mTotalAmountLeft = totalAmountLeft;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof LegacyAttendance) {
            LegacyAttendance otherAttendance = (LegacyAttendance) other;

            return (mTotalAmountLeft == otherAttendance.getTotalAmountLeft()
                    && mAmountAdded == otherAttendance.getAmountAdded()
                    &&  mDateOfAttendance == otherAttendance.getDateOfAttendance());
        } else {
            return false;
        }
    }

    @Override
    public int compareTo(Object another) {
        if (another instanceof  LegacyAttendance) {
            LegacyAttendance otherAttendance = (LegacyAttendance) another;
            if (getDateOfAttendance() > otherAttendance.getDateOfAttendance()) return -1;
            if (getDateOfAttendance() < otherAttendance.getDateOfAttendance()) return 1;
            return 0;
        }
        return 0;
    }

    public static void reCalculateAttendances(LegacyStudent student) {
        if (student.getAttendanceList() != null) {
            // Iterate backwards from first date to most current
            int currentBalance = student.getStartingBalance();
//            Collections.sort(student.getAttendanceList());

            for (int i=(student.getAttendanceList().size() - 1); i >= 0; i--) {
                LegacyAttendance attendance = student.getAttendanceList().get(i);
                if (attendance.getAmountAdded() == 0) {
                    currentBalance -= student.getPricePerSession();
                } else {
                    currentBalance += (attendance.getAmountAdded() - student.getPricePerSession());
                }
                attendance.setTotalAmountLeft(currentBalance);
            }
            student.setBalance(currentBalance);
        }
    }
}