<?php

namespace App\Http\Livewire;

use Livewire\Component;

class Calendar extends Component
{
    public $records;

    public $names = [];
    public $course_types = [];
    public $course_durations = [];
    public $course_frequencies = [];
    public $course_levels = [];

    public $courts = [];
    public $instructors = [];
    public $motivations = [];

    public $name_filter = null;

    public $name = null;
    public $from = null;
    public $to = null;
    
    public $court_id = null;
    public $instructor_id = null;
    public $note = null;
    
    public $course_type_id = null;
    public $course_duration_id = null;
    public $course_frequency_id = null;
    public $course_level_id = null;

    public $motivation_id = null;

    public $festivities = [];
    public $css_festivities = '';

    public $lastDate = null;

    public $course_colors = [];

    public function mount()
    {
        $this->names = \App\Models\Calendar::orderBy('name')->groupBy('name')->pluck('name')->toArray();
        $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
        $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
        $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
        $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
        $this->courts = \App\Models\Court::select('*')->where('enabled', true)->get();
        $this->instructors = \App\Models\User::select('*')->where('level', 2)->where('enabled', true)->get();
        $this->motivations = \App\Models\Motivation::select('*')->where('enabled', true)->where('type', 'del')->get();
        $this->course_colors = \App\Models\CourseColor::pluck('hex', 'name')->toArray();

        if (isset($_GET["name_filter"]))
            $this->name_filter = $_GET["name_filter"];

    }

    public function render()
    {
        $reload = false;
        $this->records = [];
        if ($this->name_filter != null && $this->name_filter != "")
        {
            $calendars = \App\Models\Calendar::where('name', $this->name_filter)->get();
            $reload = true;
        }
        else
            $calendars = \App\Models\Calendar::get();
        foreach($calendars as $c)
        {
            $s = $c->motivation ? $c->motivation->name : '';
            $data = array('id' => $c->id, 'title' => ($c->course ? $c->course->name : $c->name) . ($c->status == 99 ? ' (annullata - ' . $s . ')' : ''), 'start' => $c->from, 'end' => $c->to);
            // if ($c->course && $c->course->color != '')
            //     $data['color'] = $c->course->color;
            if (isset($this->course_colors[$c->name]))
                $data['color'] = $this->course_colors[$c->name];
            if ($c->status == 99)
                $data['color'] = "#808080";
            $this->records[] = $data;
        }

        for ($anno = 2025; $anno <= 2036; $anno++) {

            // $background_color = "transparent";
            // $color = "black";
            $this->records[] = array('id' => 0, 'title' => 'Capodanno', 'allDay' => true, 'start' => "$anno-01-01 00:00:00", 'end' => "$anno-01-01 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Epifania', 'allDay' => true, 'start' => "$anno-01-06 00:00:00", 'end' => "$anno-01-06 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Festa della Liberazione', 'allDay' => true, 'start' => "$anno-04-25 00:00:00", 'end' => "$anno-04-25 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Festa del Lavoro', 'allDay' => true, 'start' => "$anno-05-01 00:00:00", 'end' => "$anno-05-01 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Festa della Repubblica', 'allDay' => true, 'start' => "$anno-06-02 00:00:00", 'end' => "$anno-06-02 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Ferragosto', 'allDay' => true, 'start' => "$anno-08-15 00:00:00", 'end' => "$anno-08-15 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Ognissanti', 'allDay' => true, 'start' => "$anno-11-01 00:00:00", 'end' => "$anno-11-01 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Immacolata Concezione', 'allDay' => true, 'start' => "$anno-12-08 00:00:00", 'end' => "$anno-12-08 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Natale', 'allDay' => true, 'start' => "$anno-12-25 00:00:00", 'end' => "$anno-12-25 23:59:59", 'classNames' => ["festivity"]);
            $this->records[] = array('id' => 0, 'title' => 'Santo Stefano', 'allDay' => true, 'start' => "$anno-12-26 00:00:00", 'end' => "$anno-12-26 23:59:59", 'classNames' => ["festivity"]);

            $pasqua = date("Y-m-d", easter_date($anno));
            $this->records[] = array('id' => 0, 'title' => 'Pasqua', 'allDay' => true, 'start' => "$pasqua 00:00:00", 'end' => "$pasqua 23:59:59", 'classNames' => ["festivity"]);
            $pasquetta = date("Y-m-d", strtotime("$pasqua +1 day"));
            $this->records[] = array('id' => 0, 'title' => 'Pasquetta', 'allDay' => true, 'start' => "$pasquetta 00:00:00", 'end' => "$pasquetta 23:59:59", 'classNames' => ["festivity"]);

            $this->festivities[] = "$anno-01-01";
            $this->festivities[] = "$anno-01-06";
            $this->festivities[] = "$anno-04-25";
            $this->festivities[] = "$anno-05-01";
            $this->festivities[] = "$anno-06-02";
            $this->festivities[] = "$anno-08-15";
            $this->festivities[] = "$anno-11-01";
            $this->festivities[] = "$anno-12-08";
            $this->festivities[] = "$anno-12-25";
            $this->festivities[] = "$anno-12-26";
            $this->festivities[] = "$pasqua";
            $this->festivities[] = "$pasquetta";
        }

        $this->festivities = array_map(function($date) {
            return "[data-date='$date']";
        }, $this->festivities);
        $this->css_festivities = implode(", ", $this->festivities);
        
        $this->lastDate = null;
        if (isset($_GET['last_date'])) {
            try {
                $this->lastDate = \Illuminate\Support\Facades\Date::createFromFormat('Y-m-d',  $_GET['last_date']);
            } catch (\Throwable $e) {
                $this->lastDate = null;
            }

            if (!$this->lastDate) {
                $this->lastDate = null;
            } else {
                $this->lastDate = $this->lastDate->format("Y-m-d");
            }
        }

        if ($reload)
            $this->emit('reload-calendar', ["'" . json_encode($this->records) . "'"]);

        return view('livewire.calendar');
    }

    public function createCalendar()
    {

        $calendar = new \App\Models\Calendar();
        $calendar->name = $this->name;
        $calendar->from = $this->from;
        $calendar->to = $this->to;
        // $calendar->court_id = $this->court_id != '' ? $this->court_id : null;
        // $calendar->course_id = null;
        // $calendar->course_type_id = $this->course_type_id != '' ? $this->course_type_id : null;
        // $calendar->course_duration_id = $this->course_duration_id != '' ? $this->course_duration_id : null;
        // $calendar->course_frequency_id = $this->course_frequency_id != '' ? $this->course_frequency_id : null;
        // $calendar->course_level_id = $this->course_level_id != '' ? $this->course_level_id : null;
        // $calendar->instructor_id = $this->instructor_id != '' ? $this->instructor_id : null;
        // $calendar->note = $this->note;
        $calendar->court_id = null;
        $calendar->course_id = null;
        $calendar->course_type_id = null;
        $calendar->course_duration_id = null;
        $calendar->course_frequency_id = null;
        $calendar->course_level_id = null;
        $calendar->instructor_id = null;
        $calendar->note = null;
        $calendar->status = 0;
        $calendar->manual = 1;
        $calendar->save();

        return redirect()->to('/presences?calendarId=' . $calendar->id);

    }

    public function cancelCalendar($id, $motivation_id)
    {
        $calendar = \App\Models\Calendar::findOrFail($id);
        $calendar->motivation_id = $motivation_id;
        $calendar->status = 99;
        $calendar->save();
        return redirect()->to('/calendar');
    }

    public function revertCalendarDeletion($id)
    {
        $calendar = \App\Models\Calendar::findOrFail($id);
        $calendar->motivation_id = null;
        $calendar->status = 0;
        $calendar->save();
        return redirect()->to('/calendar');
    }

}
