<?php

namespace App\Domain\Users\Observers;

use App\Models\User;
use Illuminate\Support\Arr;
// use App\Domain\Points\Models\Point;
// use App\Domain\Points\Enums\DirectionEnum;

class UserObserver
{
    public function creating(User $user)
    {
        $user->color = $this->generateColors();
        if(!$user->username){
            $user->username = 'id_' . uniqid();
        }
    }

    public function created(User $user)
    {
        // if($user->username != 'inner_systemuser_api'){
        //     $point = new Point;
        //     $point->user_id = $user->id;
        //     $point->point = 100;
        //     $point->type = 'Тестовое пополнение'; //YSV ENUM!
        //     $point->direction = DirectionEnum::COMING();
        //     $point->save();
        // }
    }

    public function saving(User $user)
    {
        $user->user_char = $this->getChar($user);
    }

    private function generateColors()
    {
        $colors = ['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3', '#03a9f4', '#00bcd4', '#009688', '#4caf50', '#8bc34a', '#ffc107', '#ff9800', '#ff5722', '#795548', '#9e9e9e', '#607d8b', '#f44331', '#e91e61', '#9c27b1', '#673ab1', '#3f51b1', '#2196f1', '#03a9f1', '#00bcd1', '#009681', '#4caf51', '#8bc341', '#ffc101', '#ff9801', '#ff5721', '#795541', '#9e9e91', '#607d81'];
        return Arr::random($colors);
    }

    private function getChar($user)
    {
        $fname = mb_strtoupper($user->first_name);
        $lname = @$user->last_name;
        if($lname){
            $lname = mb_strtoupper($lname);
            return mb_substr($fname, 0, 1) . mb_substr($lname, 0, 1);
        }
        return mb_substr($fname, 0, 1) . mb_substr($fname, 1, 1);
    }

}
