<?php

/*
 * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class Calendar {

    private $WeekDays;
    private $Month;
    private $Year;

    public function __construct($Month, $Year) {
        $this->WeekDays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
        $this->Month = $Month;
        $this->Year = $Year;
    }

    public function GetWeekDays() {
        return $this->WeekDays;
    }

    public function GetMonth() {
        return $this->Month;
    }

    public function GetYear() {
        return $this->Year;
    }

    public function SetMonth($Month): void {
        $this->Month = $Month;
    }

    public function SetYear($Year): void {
        $this->Year = $Year;
    }

    public function GetDaysOfMonth() {
        return cal_days_in_month(CAL_GREGORIAN, $this->Month, $this->Year);
    }

    public function GetWeekStart() {
        return date("l", strtotime('01 ' . $this->GetMonthName() . ' ' . $this->Year));
    }

    public function GetMonthName() {
        return date("F", mktime(null, null, null, $this->Month));
    }

    public function GenerateCalendar() {
        $HTML = '';

        $HTML .= '<div class="container">' . PHP_EOL;
        $HTML .= '<div class="row mb-2">' . PHP_EOL;
        $HTML .= '<span class="col-12 text-center font-weight-bold t-large">' . $this->GetMonthName() . ' ' . $this->Year . '</span>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '<div class="row">' . PHP_EOL;
        for ($Position = 0; $Position < count($this->WeekDays); $Position++) {
            $HTML .= '<span class="col font-weight-bold">' . substr($this->WeekDays[$Position], 0, 3) . '</span>' . PHP_EOL;
        }
        $HTML .= '</div>' . PHP_EOL;

        $DaysOfMonth = $this->GetDaysOfMonth();
        $WeekStartPosition = array_search($this->GetWeekStart(), $this->WeekDays);
        $CurrentDay = 1;
        $Stop = false;

        while (!$Stop) {
            $HTML .= '<div class="row mt-1">' . PHP_EOL;
            
            for ($Position = 0; $Position < count($this->WeekDays); $Position++) {
                if ($CurrentDay === 1 && $Position !== $WeekStartPosition) {
                    $HTML .= '<span class="col"></span>' . PHP_EOL;
                } else {
                    if ($CurrentDay > $DaysOfMonth) {
                        $HTML .= '<span class="col"></span>' . PHP_EOL;
                    } else {
                        $HTML .= '<span class="col">' . $CurrentDay . '</span>' . PHP_EOL;
                        $CurrentDay++;
                    }
                }
            }
            
            if ($CurrentDay >= $DaysOfMonth) {
                $Stop = true;
            }
            $HTML .= '</div>' . PHP_EOL;
        }

        $HTML .= '</div>' . PHP_EOL;

        return $HTML;
    }

}
