import {Injectable}      from '@angular/core';
import {Http}            from '@angular/http';
import {Subscription}    from 'rxjs/Rx';

@Injectable()

export class Utils {

  public jsonParms: any;
  public g_daysPerMonth: any;
  public g_dowOffset: any;
  public t_days: any
  public t_dates: any
  public t_dowNames: any;
  public t_monthNames: any;
  public numberOfDays: number = 7;
  public currentDate: number
  public currentMonth: number;
  public currentYear: number;
  public weekNumber: number = 8;
  public selectedDate: Date;
  public weeks: Array<Array<any>>;
  public daysPerMonth: any;
  public monthName: any;

  constructor(public http: Http) {

    this.getParms()
    let today = new Date()
    this.currentDate = today.getDate()
    this.currentMonth = today.getMonth() + 1
    this.currentYear = today.getFullYear()
    this.selectedDate = new Date()
  }

  getWeekNumber() 


  getMonth(caldate) {
    // initialize 6 (weeks) by 7 (days) array with object c_CalendarDay (instead of zero/space)
    // this was done because of array processing differences on ios
    this.weeks = new Array(6)
    for (let ii = 0; ii < 6; ii++) {
      let week = new Array(7)
      for (let jj = 0; jj < 7; jj++) {
        week[jj] = 0
      }
      this.weeks[ii] = week
    }

    let year = caldate.getFullYear();    // year (CCYY)
    let monthIndex = caldate.getMonth(); // the index of the month where 0=Jan, 1=Feb...
    let monthNumber = monthIndex + 1;    // the number of the month where 1=Jan, 2=Feb...

    // check if leap year
    if (monthNumber == 2) {
      if ((year % 100 != 0) && (year % 4 == 0) || (year % 400 == 0)) {
        this.g_daysPerMonth[1] = 29;
      }
    }

    let daysInMonth = this.daysPerMonth[monthIndex];                 // number of days in this month
    this.monthName = this.t_monthNames[monthIndex] + " " + year;  // text to appear on top
    let firstDayOfFirstWeek = this.getDOW(year, monthNumber, 1)   // get day of week of 1st of month

    let dd = 0;                          // day counter
    for (let ii = 0; ii < 6; ii++) {    // for all weeks in month
      for (let jj = 0; jj < 7; jj++) {  // for all days in week
        if (dd < daysInMonth) {
        // 1st condition - while dd is less/equal to daysInMonth
          if (( (ii == 0) && (jj > firstDayOfFirstWeek || jj == firstDayOfFirstWeek) ) || (ii > 0)) {
          // 2nd condition - if in first week and day is on/after first day, or if after first week
            dd++  // push the day counter up by 1
            this.weeks[ii][jj] = dd // move day object to week/day
          }
        }
      }
    }
  }

   // get day of week - source: https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
  getDOW(y, m, d) {
    if (m < 3) {
      y = y - 1
    }
    let dow = Math.round((y + y / 4 - y / 100 + y / 400 + this.g_dowOffset[m - 1] + d) % 7)
    return dow
  }

  getWeekArray(localDate) {
    // arr will be returned with 7 dates of the week in which localDate exists, from Sunday to Saturday
    let arr = []
    let today = new Date();
    // today is set to the localDate passed (either today's date or that chosen from the datepicker
    today.setMonth(localDate.getMonth())
    today.setFullYear(localDate.getFullYear());
    today.setDate(localDate.getDate());
    let tempDate = new Date();
    // day is the number of the day in the week (0=Sun, 1=Mon, 2=Tue...
    let day = today.getDay();
    //today.getDate gives us the actual dd in the month, subtracting day from it gives us that Sunday, which we set to
    let sum = today.getDate() - day
    tempDate.setMonth(today.getMonth());
    tempDate.setFullYear(today.getFullYear());
    if (sum < 0) {
      tempDate.setDate(1);
      sum = (sum * -1) + 1
      for (let i = 0; i < sum; i++) {
        let stamDate = new Date()
        stamDate.setFullYear(2000)
        arr.push(stamDate)
      }
    } else {
      tempDate.setDate(today.getDate() - day);
      sum = 0
    }
    let x = new Date()
    x.setMonth(tempDate.getMonth())
    x.setFullYear(tempDate.getFullYear())
    x.setDate(tempDate.getDate())
    // we put Sunday in the arr
    arr.push(x)
    // we then put the next 6 dates from Monday to Saturday
    for (let i = 1; i < 7 - sum; i++) {
      tempDate.setDate(tempDate.getDate() + 1);
      let x = new Date()
      x.setMonth(localDate.getMonth())
      x.setFullYear(localDate.getFullYear())
      x.setDate(tempDate.getDate())
      arr.push(x)
    }
    sum = today.getDate() - day
    if (sum > 25) {
      let lastDay = new Date(localDate.getFullYear(), localDate.getMonth() + 1, 0)
      let fromDay = lastDay.getDay() + 1
      for (let i = fromDay; i < 7; i++) {
        arr[i].setFullYear(2000)
      }
    }
    return arr
  }

  getMonthArray(localDate) {
    let arr = []
    let date = new Date()
    for (let i = 0; i < this.weeks[1].length; i++) {
      if (this.weeks[i]) {
        for (let j = 0; j < this.weeks[i].length; j++) {
          let day = this.weeks[i][j]
          if (day != 0) {
            date = new Date()
            date.setFullYear(localDate.getFullYear())
            date.setMonth(localDate.getMonth())
            date.setDate(day)
            arr.push(date)
          }
        }
      }
    }
    return arr
  }
  
getParms() {
    this.http.get('assets/data/parms.json')
      .map((res) => res.json())
      .subscribe(jsonParms => {
        this.jsonParms = jsonParms
        this.g_daysPerMonth = this.jsonParms.parms.daysPerMonth
        this.g_dowOffset = this.jsonParms.parms.dowOffset
        this.t_days = this.t_days
        this.t_dates = this.t_date
        this.t_monthNames = this.t_monthNames
        this.t_dowNames = this.t_dowNames
      }, (rej) => {
        console.error("Could not load parms data", rej)
      });
  }
}

{
    for (let i = 0; i < this.weeks.length; i++) {
      if (this.weeks[i].indexOf(this.currentDate) != -1) {
        this.weekNumber = i
        return
      }
    }
  }