<?php

namespace App\Repositories;



use App\Models\TravelClass;
use App\Models\TravelProgram;
use App\Models\TravelProgramDraft;

class TravelProgramRepository extends BaseRepository {


    public function __construct(TravelProgram $model)
    {
        $this->model = $model;
    }

    public function update($data)
    {
        if($data['id'] == "new"){
            $this->model = TravelProgram::createNew();
        }
        else{
            $this->model = $this->getById($data['id']);
        }
        $this->checkDraftsWeekdays($data['weekdays']);
        $data['weekdays'] = $this->model->setWeekdaysFromArray($data['weekdays']);
        $this->model->fill($data);
        $this->model->status = isset($data['status']) ? true : false;
        $this->model->save();
        return $this->model;
    }

    public function checkDraftsWeekdays($weekdays){
        if(isset($weekdays) && is_array($weekdays) && $weekdays[0] === NULL){
            $weekdays = range(0, 6);
        }
        foreach($this->model->travel_program_drafts as $travel_program_draft){
            if($travel_program_draft->weekdays !== NULL){
                $wd = [];
                foreach ($travel_program_draft->weekdays as $weekday){
                    if(in_array($weekday, $weekdays)){
                        $wd[] = $weekday;
                    }
                }
                if(count($wd)){
                    $travel_program_draft->weekdays = $wd;
                }else{
                    $travel_program_draft->weekdays = NULL;
                }
                $travel_program_draft->save();
            }
        }
    }

    public function updateClass($data)
    {
        if($data['id'] == "new"){
            $travel_class = TravelClass::create([
                'program_id' => $data['program_id'],
                'name' => $data['name'],
                'standard' => isset($data['standard']) ? true : false,
                'description' => $data['description'],
            ]);
        }else {
            $travel_class = TravelClass::findOrFail($data['id']);
            $travel_class->name = $data['name'];
            $travel_class->standard = isset($data['standard']) ? true : false;
            $travel_class->description = $data['description'];
            $travel_class->save();
        }
        return $travel_class;
    }

    public function updateDraft($data)
    {
        if(isset($data['weekdays']) && count($data['weekdays']) > 1 && in_array(NULL, $data['weekdays'])){
            $data['weekdays'] = array_filter($data['weekdays'], 'is_numeric');
            sort($data['weekdays']);
        }
        if($data['id'] == "new"){
            $travel_program_draft = TravelProgramDraft::create([
                'travel_program_id' => $data['travel_program_id'],
                'travel_class_id' => $data['travel_class_id'],
                'draft_id' => $data['draft_id'],
                'weekdays' => isset($data['weekdays']) ? $data['weekdays'] : null,
            ]);
        }else{
            $travel_program_draft = TravelProgramDraft::findOrFail($data['id']);
            $travel_program_draft->draft_id = $data['draft_id'];
            $travel_program_draft->travel_class_id = $data['travel_class_id'];
            $travel_program_draft->weekdays = isset($data['weekdays']) ? $data['weekdays'] : null;
            $travel_program_draft->save();
        }
        return $travel_program_draft;
    }



}