<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Schedule;
use function GuzzleHttp\json_encode;
use PDF;

class ScheduleController extends Controller
{
    public function index()
    {
        return view('admin.configs.schedule',['configs' => \App\Config::all()]);
    }

    public function update()
    {
        $configs = ['admin_periode_nb'];

        foreach ($configs as $config) {
            $c = \App\Config::all()->where('name',$config)->first();
            $c->data = [request($config)];
            $c->save();
        }

        $new_admin_periode_begin = [];
        $new_admin_periode_end = [];
        for ($i=1; $i <= request('admin_periode_nb'); $i++) { 
            if(request('admin_periode_begin_'.$i))
            {
                $new_admin_periode_begin[$i] = request('admin_periode_begin_'.$i);
            }
            else 
            {
                $new_admin_periode_begin[$i] = "00:00";
            }
            if(request('admin_periode_end_'.$i))
            {
                $new_admin_periode_end[$i] = request('admin_periode_end_'.$i);
            }
            else
            {
                $new_admin_periode_end[$i] = "00:00";
            }
        }

        $temp = \App\Config::all()->where('name','admin_periode_begin')->first();
        $temp->data = $new_admin_periode_begin;
        $temp->save();

        $temp = \App\Config::all()->where('name','admin_periode_end')->first();
        $temp->data = $new_admin_periode_end;
        $temp->save();

        return redirect('/admin/config/schedule')->with('success','Modification sauvegarder avec succès !');
    }

    public function apiIndex()
    {
        $start = strtotime(request()->start);
        $end = strtotime(request()->end);
        $allschedules = Schedule::all();

        $allevents = \App\Event::all();
        $events = [];
        $jsonevents = [];
        $schedules = [];

        foreach ($allschedules as $schedule) {
            if(strtotime($schedule->date) >= $start && strtotime($schedule->date) <= $end) {
                array_push($schedules,$schedule);
            }
        }

        foreach ($allevents as $event) {
            if(strtotime($event->date_begin) >= $start && strtotime($event->date_begin) <= $end) {
                array_push($events,$event);
            }
            else if(strtotime($event->date_end) >= $start && strtotime($event->date_end) <= $end) {
                array_push($events,$event);
            }
        }
        

        foreach ($schedules as $schedule) {
            
            $color = $this->getColor($schedule->type);
            
            $event = [
                'title' => $schedule->data['event_name'],
                'start' => $schedule->date.'T'.$schedule->data['event_begin_time'],
                'end' => $schedule->date.'T'.$schedule->data['event_end_time'],
                'color' => $color,
                'source' => 'schedule',
                'id' => $schedule->id
            ];
            array_push($jsonevents,$event);
        }

        foreach ($events as $event) {
            
            $color = $this->getColor($event->type);
            
            $myevent = [
                'title' => $event->name,
                'start' => date('c',strtotime($event->date_begin)),
                'end' => date('c',strtotime($event->date_end)),
                'color' => $color,
                'extraParams' => [
                    'db_type' => 'event'],
                'id' => $event->id
            ];
            array_push($jsonevents,$myevent);
        }

        return json_encode($jsonevents);
    }

    public function loadModal($id,$db_type)
    {
        if($db_type == "event")
        {
            $event = \App\Event::find($id);
        }
        else
        {
            $event = \App\Schedule::find($id);
        }
        return view('public.modal.schedule',['event' => $event]);
    }

    public function loadModalFull($id,$db_type)
    {
        if($db_type == "event")
        {
            $event = \App\Event::find($id);
        }
        else
        {
            $event = \App\Schedule::find($id);
        }
        return view('admin.schedule.modal.show',['event' => $event]);
    }

    public function getColor($type)
    {
        $activity = \App\ComplementaryActivity::all();
        $color = 'blue';
        switch ($type) {
            case 'regular':
                $color = 'orange';
                break;
            
            case 'pilotage':
               $color = '#58D3F7';
               break;
            
            case 'drill':
                $color = 'blue';
                break;
            
            case 'music':
                $color = 'green';
                break;
            
            case 'biathlon':
                $color = 'red';
                break;
            
            case 'marksmanship':
                $color = 'grey';
                break;
            
            case 'founding':
                $color = '#00FF40';
                break;
            
            case 'volunteer':
                $color = '#DF0174';
                break;
                
            case 'other':
                $color = '#DF0174';
                break;
            
            default:
                if ($activity->find($type)) {
                    $color = $activity->find($type)->calendar_color;
                }
                break;
        };
        return $color;
    }

    public function printtopdf($id)
    {
        $event = \App\Event::find($id);
        $pdf = PDF::loadView('admin.schedule.modal.show',['event' => $event]);
		return $pdf->download($event->date_begin.'.pdf');
    }

    public function create($date)
    {
        $date = str_replace('/','-',$date);
        return view('admin.schedule.event.add',['date' => $date]);
    }

    public function loadModalDefautType($type,$date)
    {

        $activity = \App\ComplementaryActivity::find($type);
        $begin_time = $date." ".$activity->begin_time;
        $end_time = $date." ".$activity->end_time;
        return view('admin.schedule.modal.add',[
            'activity' => \App\ComplementaryActivity::find($type), 
            'begin_time' => $begin_time,
            'end_time' => $end_time
            ]);
    }

    public function delete($id)
    {
        $event  = \App\Event::find($id);

        $event->delete();
    }
}
