<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Exercise extends Model
{
    protected $fillable = [
        "name", "slug", "description", "modality", "level", "url_video_predefined"
    ];

    protected $appends = ["type","equipment_type"];

    protected $hidden = ["exercise_types","equipment"];

    /**
     * ATRIBUTS FICTICIS
     */
    public function getTypeAttribute() {
        $type = "";
        $types = ($this->exercise_types)->toArray();

        for($i=0;$i<count($types);$i++) {
            $type = $type . $types[$i]['name'];
            if(($i+1)<count($types)) $type = $type . ", ";
        }
        if($type == "") return null;
        return $type;
    }

    public function getEquipmentTypeAttribute() {
        $type = "";
        $types = ($this-> equipment)->toArray();

        for($i=0;$i<count($types);$i++) {
            $type = $type . $types[$i]['name'];
            if(($i+1)<count($types)) $type = $type . ", ";
        }
        if($type == "") return null;
        return $type;
    }

    public function exercise_types(){
        return $this->belongsToMany('App\ExerciseType','exercise_exercise_types');
    }

    public function equipment(){
        return $this->belongsToMany('App\Equipment','exercise_equipments');
    }
}
