<?php

class Tournament extends Model
{
    protected $dbTable = "tournaments";
    protected $dbFields = array(
        'registered' => ['type' => 'datetime', 'required', 'unique', 'autoValMethod' => 'getDateTime'],
        'name' =>  ['type' => 'text', 'required'],
        'date' =>   ['type' => 'datetime','required'],
        'duration' =>   ['type' => 'int'], //in days
        'text' => ['type' => 'text'],
        'url' =>   ['type' => 'text'], //if there is one
        'logo' =>  ['type' => 'text', 'default' => 'https://pictshare.net/prrnrk.jpg'],
        'admins' => ['type'=> 'array', 'default'=>[]],
        'members' => ['type'=> 'array', 'default'=>[]],
        'runs' => ['type'=> 'array', 'default'=>[]],
    );

    function joinUser($tournament = false)
    {
        if ($tournament == false)
            $tournament = $this->id;

        if (!$this->exists($tournament))
            return false;
        else {
            if (!in_array($_SESSION['user']->id, $this->data['members']))
                $this->data['members'][] = $_SESSION['user']->id;
            if(!in_array($tournament,$_SESSION['user']->data['tournaments']))
            {
                $_SESSION['user']->data['tournaments'][] = $tournament;
                $_SESSION['user']->save();
            }
            $this->save();
        }
    }

    function removeUser($tournament = false)
    {
        if ($tournament == false)
            $tournament = $this->id;

        if (!$this->exists($tournament))
            return false;
        else {
            if (in_array($_SESSION['user']->id, $this->data['members']))
                $this->data['members'] = array_diff($this->data['members'],[$_SESSION['user']->id]);
            if(in_array($tournament,$_SESSION['user']->data['tournaments']))
            {
                $_SESSION['user']->data['tournaments'] = array_diff($_SESSION['user']->data['tournaments'],[$tournament]);
                $_SESSION['user']->save();
            }
            $this->save();
        }
    }

    function isMyEvent($tournament = false)
    {
        if ($tournament == false)
            $tournament = $this->id;

        if (!$this->exists($tournament))
            return false;
        else {
            if (in_array($tournament, $_SESSION['user']->data['tournaments']))
                return true;
            else return false;
        }
    }

    function amIAdmin($tournament = false)
    {
        if ($tournament == false)
            $tournament = $this->id;

        if (!$this->exists($tournament))
            return false;
        else {
            if (in_array($_SESSION['user']->id, $this->data['admins']))
                return true;
            else return false;
        }
    }

    function getAdmins($tournament = false)
    {
        if ($tournament == false)
            $tournament = $this->id;

        if (!$this->exists($tournament))
            return false;
        else {
            $admins = [];
            foreach($this->data['admins'] as $admin)
            {
                $u = new User();
                $u->load($admin);
                $admins[] = $u->getDataFiltered();
            }
            return $admins;
        }
    }

    function getMembers($tournament = false)
    {
        if ($tournament == false)
            $tournament = $this->id;

        if (!$this->exists($tournament))
            return false;
        else {
            $members = [];
            foreach($this->data['members'] as $member)
            {
                $u = new User();
                $u->load($member);
                $members[] = $u->getDataFiltered();
            }
            return $members;
        }
    }
}
