{"size": 1975, "ext": "js", "max_stars_repo_path": "src/client/scripts/calendar/Month.js", "max_stars_repo_name": "woowa-techcamp-2020/hkb-13", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/client/scripts/calendar/Month.js", "max_issues_repo_name": "woowa-techcamp-2020/hkb-13", "max_issues_repo_issues_event_min_datetime": "2020-07-27T08:03:25.000Z", "max_issues_repo_issues_event_max_datetime": "2020-08-06T06:38:17.000Z", "max_forks_repo_path": "src/client/scripts/calendar/Month.js", "max_forks_repo_name": "woowa-techcamp-2020/hkb-13", "max_forks_repo_forks_event_min_datetime": "2020-08-04T00:54:35.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-10T04:58:06.000Z"}
{"max_stars_count": null, "max_issues_count": 26, "max_forks_count": 2, "avg_line_length": 21.2365591398, "max_line_length": 63, "alphanum_fraction": 0.5513924051}
class Month {
  constructor(year, month, monthData) {
    this.year = year
    this.month = month
    this.data = monthData
    this.weeks = []
    this.init()
  }

  init() {
    this.setWeeks()
  }

  setWeeks() {
    this.weeks = []

    let date = new Date(this.year, this.month - 1, 1)
    let nextMonthFirstDate = new Date(this.year, this.month, 1)
    while (true) {
      let week = []
      while (true) {
        let day = date.getDay()
        week.push({
          year: date.getFullYear(),
          month: date.getMonth() + 1,
          date: date.getDate(),
          income: this.getIncome(date.getDate()),
          expense: this.getExpense(date.getDate()),
        })
        date = new Date(date.setDate(date.getDate() + 1))
        if (day === 6) break
        if (date >= nextMonthFirstDate) break
      }
      this.weeks.push(week)
      if (date >= nextMonthFirstDate) break
    }
  }

  getWeeks() {
    return this.weeks
  }

  getDaily() {
    return this.weeks.flat()
  }

  getFirstWeek() {
    return this.weeks[0]
  }

  getLastWeek() {
    return this.weeks[this.weeks.length - 1]
  }

  getIncome(date) {
    const idx = this.data.findIndex(
      (data) => data.date == date && data.type == 'income'
    )
    if (!this.data[idx]) return ''
    return this.data[idx].price
  }

  getExpense(date) {
    const idx = this.data.findIndex(
      (data) => data.date == date && data.type == 'expense'
    )
    if (!this.data[idx]) return ''
    return this.data[idx].price
  }

  getTotalIncome() {
    let totalIncome = 0
    this.weeks.forEach((week) => {
      week.forEach((day) => {
        if (day.income !== '') totalIncome += day.income
      })
    })
    return parseInt(totalIncome)
  }

  getTotalExpense() {
    let totalExpense = 0
    this.weeks.forEach((week) => {
      week.forEach((day) => {
        if (day.expense !== '') totalExpense += day.expense
      })
    })
    return parseInt(totalExpense)
  }
}

export { Month }
