<?php

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


class CharacterModel
{
    
    private CharacterGateway $gateway;

    public function __construct(characterGateway $gw)
    {
        $this -> gateway = $gw;
    }
    
    public function createCharacter(int $id_character, string $name , string $img_char) : bool
    {
        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['id_character'],
                $c['name'],
                $c['img_path']
            );
        return null;
    }

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

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

        $characters = [];

        foreach ($c as $character)
        {
            $characters[] = new CharacterEntity(
                $character['id_character'],
                $character['name'],
                $character['img_path']
            );
        }
        return $characters;
    }

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

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

}