<?php

namespace Amwdeveloper\controller;

use Amwdeveloper\core\session;
use Amwdeveloper\model\layer;
use Amwdeveloper\model\message;

class user extends layer
{
    public $session;
    public $message;


    public function sessionuser()
    {
        $this->session = new session();
        return $this->session->useraccount;
    }

    public function messageuser()
    {
        $this->message = new message();
        return $this->message;
    }

    public function coins(): array
    {
        if (coins) {
            $listcoins = array();
            foreach (coins as $coin) {
                $select = $this->table($coin['table'])->columns($coin['column_coin'])->where("{$coin['column_account']} = :acc", "acc={$this->sessionuser()}")->get();
                $listcoins[] = [
                    "amount" => $select->{$coin['column_coin']} ?? 0,
                    "name" => $coin['name_coin'] ?? '-'
                ];
            }
            return $listcoins;
        }
    }

    public function myaccount(): array
    {
        $myaccount = $this->table("MEMB_INFO")->columns("id_ref,memb_name,mail_addr," . config['column_vip'] . ",AccountExpireDate")->where("memb___id = :mi", "mi={$this->sessionuser()}")->get();
        $ip = $_SERVER['REMOTE_ADDR'];
        $indications = $this->table("amw_indication")->where("user_ref = :r AND ip != :ip", "r={$this->sessionuser()}&ip={$ip}")->count()->amount;

        $information = [
            "name" => $myaccount->memb_name,
            "email" => $myaccount->mail_addr,
            "vip" => vip[$myaccount->{config['column_vip']}],
            "coins" => $this->coins(),
            "expiration" => date('d/m/Y H:i:s', strtotime($myaccount->AccountExpireDate)),
            'link_indication' => __redirect('register') . "?ref={$myaccount->id_ref}",
            'indications' => $indications
        ];

        return $information;
    }

    public function mycharacters(): array
    {
        $characters = $this->table("Character")->columns("Name as name, Imagem as image")->where("AccountID = :mi", "mi={$this->sessionuser()}")->all();
        $mychars = array();
        foreach ($characters as $char) {
            $mychars[] = [
                'name' => $char->name,
                'image' => $char->image
            ];
        }

        return $mychars;
    }

    public function profilecharacter(array $nick)
    {
        global $route;
        $char = filter_var($nick['char'], FILTER_SANITIZE_SPECIAL_CHARS);

        if (empty($char)) $route->redirect("user/info");

        $verifychar = $this->table("Character")->columns("*")->where("Name = :name AND AccountID = :acc", "name={$char}&acc={$this->sessionuser()}")->get();

        if (empty($verifychar))  $route->redirect("user/info");

        $profile = array(
            'name' => $verifychar->Name,
            'strength' => $verifychar->Strength,
            'dexterity' => $verifychar->Dexterity,
            'energy' => $verifychar->Energy,
            'vitality' => $verifychar->Vitality,
            'class' => $verifychar->Class,
            'level' => $verifychar->cLevel,
            'command' => ($verifychar->Class == 64 or $verifychar->Class == 65  or $verifychar->Class == 66) ? $verifychar->Leadership : null,
            'image' => $verifychar->Imagem,
            'map' => $verifychar->MapNumber,
            'mapx' => $verifychar->MapPosX,
            'mapy' => $verifychar->MapPosY,
            'resets' => $verifychar->{columns_score_perfil['resets']} ?? 0,
            'mresets' => $verifychar->{columns_score_perfil['mresets']} ?? 0,
            'killer' => $verifychar->{columns_score_perfil['killer']} ?? 0,
            'hero' => $verifychar->{columns_score_perfil['hero']} ?? 0
        );
        return $profile;
    }
}
