<?php


namespace App\Repositories;


use App\Exercise;
use App\TrainingSession;
use App\TrainingWorkout;
use App\TrainingWorkoutLine;
use App\UserBenchmark;
use Carbon\Carbon;

class ExerciseRepository implements RepositoryInterface
{
    public static function all() {
        return Exercise::all();
    }

    public static function create(array $data){
        return Exercise::create($data);
    }

    public static function update(array $data, $id){
        Exercise::findOrFail($id)->update($data);
        return Exercise::findOrFail($id);
    }

    public static function delete($id){
        return Exercise::destroy($id);
    }

    public static function find($id){
        return Exercise::findOrFail($id);
    }

    public static function findByName($name){
        return Exercise::where('name','=',$name)->first();
    }

    public static function filter($params) {
        $sort_dir = $params['sort_dir'];
        $sort_col = $params['sort_col'];
        $name = $params['name'];
        $modality = $params['modality'];
        $exercise_types = $params['exercise_types'];
        $difficulty = $params['difficulty'];
        $equipment = $params['equipment'];

        $e = Exercise::where(function($n) use ($name) {
            $n->where('name','LIKE','%'.$name.'%')
                ->orWhere('name','LIKE',$name.'%')
                ->orWhere('name','LIKE','%'.$name);
        })->whereIn('modality',$modality)
        ->where('level','<=',$difficulty)->orderBy($sort_col,$sort_dir)->get();

        if ($exercise_types!=null || $equipment!=null) {

            $e2 = [];
            foreach ($e as $exercise) {
                $containsExerciseType = false;
                $containsEquipmentType = false;
                if ($exercise_types!=null && !$containsExerciseType && $exercise->type!=null) {
                    $containsExerciseType = array_intersect(explode(', ',$exercise->type),$exercise_types);
                } else $containsExerciseType = true;
                if ($equipment!=null && !$containsEquipmentType && $exercise->equipment_type!=null) {
                    $containsEquipmentType = array_intersect(explode(', ',$exercise->equipment_type),$equipment);
                } else $containsEquipmentType = true;
                if ($containsExerciseType && $containsEquipmentType) array_push($e2,$exercise);
            }

            return $e2;
        } else return $e;
    }

    public static function benchmarks($id,$id_user) {
        $ub = UserBenchmark::where('user_id',$id_user)
            ->get();

        $ub = collect($ub)->sortBy('training_session.date_start');

        $e = ExerciseRepository::find($id);

        $sesiones = [];
        $workouts = [];
        $grafic = [];
        foreach ($ub as $b) {
            foreach ($b->user_benchmark_sections as $section) {
                foreach ($section->user_benchmark_lines as $line) {
                    if (TrainingWorkoutLine::find($line->training_workout_line_id)->exercise_id == $e->id) {
                        array_push($sesiones, $b);
                        array_push($workouts, TrainingWorkout::find($b->training_workout_id));
                        if ($line->val!=null) {
                            $valor = $line->units=='lb' ? ($line->val*2.20462) : ($line->units=='mile' ? $line->val*0.000621371 : $line->val);
                            array_push($grafic,
                                [
                                    "name" => Carbon::createFromFormat('Y-m-d H:i:s',TrainingSession::find($b->training_session_id)->day)->format('d/m/Y'),
                                    "value" => (int)number_format($valor,2,',','')
                                ]
                            );
                        }

                    }
                }
            }
        }

        return [
            "benchmarks" => $sesiones,
            "workouts" => $workouts,
            "graph" => $grafic
        ];
    }
}
