<?php

namespace App\Models;

use App\Traits\HasFiles;
use App\Traits\HasImages;
use App\Traits\HasSortsTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Color\Hex;


class Combustible extends Model
{
    use HasFactory, HasSortsTrait, HasFiles, HasImages;

    protected $table = "combustibles";

    protected $fillable = [
        'titulo',
        'slug',
        'eslogan',
        'descripcion',
        'descripcion_corto',
        'tipo_hero',
        'video_url',
        'color',
        'orden',
        'estado',
        'caracteristicas'
    ];

    protected $dates = ['created_at', 'updated_at'];

    protected $casts = [
        'created_at' => 'datetime:d-m-Y H:i:s',
        'updated_at' => 'datetime:d-m-Y H:i:s',
        'caracteristicas' => 'array'
    ];

    protected $appends = [
        'imagen',
        'imagen_hero',
        'imagen_ficha_tecnica',
        'color_darker',
        'color_darker_rgba',
        'video_url_formateado'
    ];

    public function getColorDarkerAttribute()
    {
        if ($this->color) {
            $hexColor = $this->color;
            $percentage = 20;
            // Convierte el color hexadecimal en RGB
            $r = hexdec(substr($hexColor, 1, 2));
            $g = hexdec(substr($hexColor, 3, 2));
            $b = hexdec(substr($hexColor, 5, 2));

            // Calcula el nuevo color oscurecido
            $r = $r - ($r * $percentage / 100);
            $g = $g - ($g * $percentage / 100);
            $b = $b - ($b * $percentage / 100);

            // Asegúrate de que los valores RGB estén en el rango correcto
            $r = max(0, min(255, $r));
            $g = max(0, min(255, $g));
            $b = max(0, min(255, $b));

            // Convierte el nuevo color RGB en formato hexadecimal
            $newHexColor = sprintf("#%02x%02x%02x", $r, $g, $b);

            return $newHexColor;
        }
    }

    public function getColorDarkerRgbaAttribute()
    {
        if ($this->color) {
            $hexColor = $this->color;
            $percentage = 0;
            // Convierte el color hexadecimal en RGB
            $r = hexdec(substr($hexColor, 1, 2));
            $g = hexdec(substr($hexColor, 3, 2));
            $b = hexdec(substr($hexColor, 5, 2));

            // Calcula el nuevo color oscurecido
            $r = $r - ($r * $percentage / 100);
            $g = $g - ($g * $percentage / 100);
            $b = $b - ($b * $percentage / 100);

            // Asegúrate de que los valores RGB estén en el rango correcto
            $r = max(0, min(255, $r));
            $g = max(0, min(255, $g));
            $b = max(0, min(255, $b));

            $r = round($r);
            $g = round($g);
            $b = round($b);
            $newRGBColor = "rgba($r, $g, $b, 0.7)";
            return $newRGBColor;
        }
    }

    public function setTituloAttribute($value)
    {
        $this->attributes['titulo'] = ucfirst(trim($value));
        $this->attributes['slug'] =  str_slug(trim($value));
    }

    public function getImagenAttribute()
    {
        return $this->getThumbnail();
    }
    public function getImagenHeroAttribute()
    {
        return $this->getThumbnail('hero');
    }

    public function getImagenFichaTecnicaAttribute()
    {
        return $this->getThumbnail('ficha_tecnica', 'full');
    }

    public function getVideoUrlFormateadoAttribute()
    {

        if (!$this->video_url || !strlen(trim($this->video_url)))
            return null;

        //? PASO 1 - Extraemos el video code
        $video_code = "";
        // Extraer el código del video de una URL "https://www.youtube.com/watch?v=Z34BF9PCfYg"
        if (preg_match('/\?v=([a-zA-Z0-9_-]+)/', $this->video_url, $matches)) {
            $video_code = $matches[1];
        } else {
            // Extraer el código del video de una URL "https://youtu.be/Z34BF9PCfYg"
            preg_match('/\/([a-zA-Z0-9_-]+)$/', $this->video_url, $matches);
            $video_code = $matches[1];
        }

        $url = "https://www.youtube.com/embed/{$video_code}?controls=0&showinfo=0&rel=0&autoplay=1&loop=1&mute=1&end=230&playlist={$video_code}";
        return $url;
    }
}
