<?php

namespace Model;
use Entity\CharacterEntity;
use Gateway\CharacterGateway;
use Gateway\Gateway;


class CharacterModel extends Model
{
    
    public function createCharacter(int $id_character, string $name , int $img_char) : bool
    {
        return $this -> gateway -> create($id_character, $name, $img_char);
    }

    public function createCharacterWithoutId(string $name , int $img_char) : bool
    {
        $id_character = $this -> gateway -> getLastId();
        return $this -> gateway -> create($id_character, $name, $img_char);
    }

    public function getCharacterById(int $id_character) : ?CharacterEntity
    {
        $c = $this -> gateway -> findById($id_character);
        if ($c)
            return new CharacterEntity(
                $c[0]['id_caracter'],
                $c[0]['caracter'],
                $c[0]['id_img']
            );
        return null;
    }

    public function getAllPerso() :array{
        $res = $this->gateway->findAll();
        foreach($res as $c){
            $charac[] = new CharacterEntity(
                $c['id_caracter'],
                $c['caracter'],
                $c['id_img']
            );
        }
        return $charac;
    }

    public function getCharacterByName(string $name) : ?CharacterEntity
    {
        $c = $this -> gateway -> findByName($name);
        if ($c)
            return new CharacterEntity(
                $c[0]['id_caracter'],
                $c[0]['caracter'],
                $c[0]['id_img']
            );
        return null;
    }

    public function getAllCharacters() : array
    {
        $c = $this -> gateway -> findAll();

        $characters = [];

        foreach ($c as $character)
        {
            $characters[] = new CharacterEntity(
                $character['id_caracter'],
                $character['caracter'],
                $character['id_img']
            );
        }
        return $characters;
    }

    public function deleteCharacter(int $id_character) : bool
    {
        return $this -> gateway -> delete($id_character);
    }

    public function updateCharacter(int $id_character, string $name, int $img_char) : bool
    {
        return  $this -> gateway -> update($id_character, $name, $img_char);
    }

}