package teammemes.tritonbudget.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * Created by andrewli on 2/26/17.
 */

public class HistoryDataSource extends BaseDataSource{

    public HistoryDataSource(Context context) {
        super(context);
    }

    public TranHistory createTransaction(TranHistory trans) {
        ContentValues values = new ContentValues();
        values.put(HistoryDB.NAME, trans.getName());
        values.put(HistoryDB.QUANTITY, trans.getQuantity());
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        values.put(HistoryDB.TDATE, dformat.format(trans.getTdate()));
        values.put(MenuDB.COSTCOL, trans.getCost());
        long insertId = database.insert(HistoryDB.Table_Transaction, null,
                values);
        Cursor cursor = database.query(HistoryDB.Table_Transaction,
                HistoryDB.allColumns, MenuDB.IDCOL + " = " + insertId, null,
                null, null, null);
        cursor.moveToFirst();
        TranHistory newTran = cursorToTransaction(cursor);
        cursor.close();
        return newTran;
    }

    public List<TranHistory> getAllTransaction() {
        List<TranHistory> transactions = new ArrayList<TranHistory>();
        Cursor cursor = database.query(HistoryDB.Table_Transaction, HistoryDB.allColumns, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            TranHistory transaction = cursorToTransaction(cursor);
            transactions.add(transaction);
            cursor.moveToNext();
        }
        // make sure to close the cursor
        cursor.close();
        return transactions;
    }

  public double[] getTransactionByWeek() {
        double[] cost = new double[7];
        List<TranHistory> histories= this.getAllTransaction();
        int currDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        for(int i=0;i<histories.size();i++)
        {
            Date day = histories.get(i).getTdate();
            Calendar cal = Calendar.getInstance();
            cal.setTime(day);
            int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
            if(0<currDay-dayOfYear && currDay-dayOfYear<8)
            {
                cost[currDay-dayOfYear-1]+=histories.get(i).getCost();
            }
        }
        return cost;
    }
    public double[] getTransactionByMonth() {
        double[] cost = new double[4];
        List<TranHistory> histories = this.getAllTransaction();
        int currDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        for (int i = 0; i < histories.size(); i++) {
            Date day = histories.get(i).getTdate();
            Calendar cal = Calendar.getInstance();
            cal.setTime(day);
            int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
            if (0 < currDay - dayOfYear && currDay - dayOfYear < 8) {
                cost[0] += histories.get(i).getCost();
            }
            if (7 < currDay - dayOfYear && currDay - dayOfYear < 15) {
                cost[1] += histories.get(i).getCost();
            }
            if (14 < currDay - dayOfYear && currDay - dayOfYear < 22) {
                cost[2] += histories.get(i).getCost();
            }
            if (21 < currDay - dayOfYear && currDay - dayOfYear < 29) {
                cost[3] += histories.get(i).getCost();
            }
        }
        return cost;
    }

    private TranHistory cursorToTransaction(Cursor cursor) {
        TranHistory transaction = new TranHistory();
        try {
            transaction.setId(cursor.getInt(cursor.getColumnIndex(HistoryDB.ID)));
            transaction.setName(cursor.getString(cursor.getColumnIndex(HistoryDB.NAME)));
            transaction.setQuantity(cursor.getInt(cursor.getColumnIndex(HistoryDB.QUANTITY)));
            SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            transaction.setTdate(dformat.parse(cursor.getString(cursor.getColumnIndex(HistoryDB.TDATE))));
            transaction.setCost(cursor.getDouble(cursor.getColumnIndex(MenuDB.COSTCOL)));
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return transaction;
    }
}

@@ -3589,24 +3589,1378 @@
  cost;%0A    %7D
+%0A    public double getThisWeekTotal() %7B%0A        double cost = 0;%0A        List%3CTranHistory%3E histories= this.getAllTransaction();%0A        int currMonth = Calendar.getInstance().get(Calendar.MONTH);%0A        int currWeek = Calendar.getInstance().get(Calendar.WEEK_OF_MONTH);%0A        for(int i=0;i%3Chistories.size();i++)%0A        %7B%0A            Date day = histories.get(i).getTdate();%0A            Calendar cal = Calendar.getInstance();%0A            cal.setTime(day);%0A            int currmon = cal.get(Calendar.MONTH);%0A            int currwek = Calendar.getInstance().get(Calendar.WEEK_OF_MONTH);%0A            if(currmon==currMonth&&currWeek==currwek)%0A            %7B%0A                cost += histories.get(i).getCost();%0A            %7D%0A        %7D%0A        return cost;%0A    %7D%0A    public double getDailyAverage() %7B%0A        double cost = 0;%0A        List%3CTranHistory%3E histories= this.getAllTransaction();%0A        int currDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);%0A        for(int i=0;i%3Chistories.size();i++)%0A        %7B%0A            cost += histories.get(i).getCost();%0A        %7D%0A        Calendar cal = Calendar.getInstance();%0A        if(currDay%3C167)%0A        %7B%0A            return cost/(167-currDay);%0A        %7D%0A        else if(currDay%3E268)%0A        %7B%0A            return cost/(167+365-currDay)%0A        %7D%0A        else%0A        %7B%0A            return 999999;%0A        %7D%0A    %7D
 %0A%0A    privat

Update HistoryDataSource.java