<?php

namespace WoWPress\Models;

use Wenprise\Eloquent\Model;
use WoWPress\Api\WoWAudit;

class Signup extends Model
{

    protected $table = "wowpress_signups";
    protected $primaryKey = 'ID';
    protected $guarded = ['ID'];
    protected $statusList = [];

    private $api;

    protected $roleList = [];

    public $status = "Unknown";
    public $role = "Unknown";

    public function __construct()
    {
        $this->statusList = [
            'Unknown' => (object)[
                'selectable' => false,
                'name' => 'Keine Rückmeldung',
                'icon' => 'o-no-symbol',
                'color' => 'slate-500',
            ],
            'Present' => (object)[
                'selectable' => true,
                'name' => 'Anwesend',
                'icon' => 'o-check-circle',
                'color' => 'green-500',
            ],
            'Absent' => (object)[
                'selectable' => true,
                'name' => 'Abwesend',
                'icon' => 'o-x-circle',
                'color' => 'red-500',
            ],
            'Late' => (object)[
                'selectable' => true,
                'name' => 'Verspätet',
                'icon' => 'o-clock',
                'color' => 'orange-500',
            ],
            'Tentative' => (object)[
                'selectable' => true,
                'name' => 'Ersatzbank',
                'icon' => 'o-question-mark-circle',
                'color' => 'yellow-500',
            ],
            'Selected' => (object)[
                'selectable' => false,
                'name' => 'Bestätigt',
                'icon' => 's-star',
                'color' => 'paladin',
            ],
            'Too Late' => (object)[
                'selectable' => false,
                'name' => 'Anmeldung Vergessen',
                'icon' => 'o-face-frown',
                'color' => 'deathknight',
            ],
        ];

        $this->roleList = [
            'Unknown' => (object)[
                'selectable' => false,
                'name' => 'Keine Rolle',
                'icon' => 'o-no-symbol',
                'color' => 'slate-500',
            ],
            'Tank' => (object)[
                'selectable' => true,
                'name' => 'Tank',
                'icon' => 'o-shield-exclamation',
                'color' => 'white',
            ],
            'Heal' => (object)[
                'selectable' => true,
                'name' => 'Heilung',
                'icon' => 'o-heart',
                'color' => 'white',
            ],
            'Ranged' => (object)[
                'selectable' => true,
                'name' => 'Ranged',
                'icon' => 'o-bolt',
                'color' => 'white',
            ],
            'Melee' => (object)[
                'selectable' => true,
                'name' => 'Melee',
                'icon' => 'o-hand-raised',
                'color' => 'white',
            ],
        ];

        $options = get_option('wowpress_api');
        if (isset($options['bnet'])) {
            $key = get_option('wowpress_api')['wowaudit']['key'];
        } else {
            return false;
        }

        $this->api = new WoWAudit(null, $key);
    }

    public function character()
    {
        return $this->belongsTo(Character::class);
    }

    public function raid()
    {
        return $this->belongsTo(Raid::class);
    }

    public function showForm($showRole = false, $showStatus = false, $vertical = false, $doSignup = true, $showComment = false)
    {
        $tooLate = $this->raid->status == "Locked";
        $doSignup = $doSignup && !$tooLate;
        $showComment = $showComment && $this->comment;

        if ($this->status == "Unknown" && $tooLate) {
            $this->setStatus('Too Late');
            $showStatus = true;
            $showRole = false;
        }


        require(get_template_directory() . "/template-parts/components/raid-signup.php");
    }

    public function getStatusList($selectable = false)
    {
        $out = [];
        foreach ($this->statusList as $key => $status) {
            if ($selectable) {
                if ($status->selectable) {
                    $out[$key] = $status;
                }
            } else {
                $out[$key] = $status;
            }
        }

        return $out;
    }

    public function getRoleList()
    {
        $out = [];
        foreach ($this->roleList as $key => $status) {
            if ($status->selectable) {
                $out[$key] = $status;
            }
        }

        return $out;
    }


    public function getStatus()
    {
        $tooLate = $this->raid->status == "Locked";
        if($tooLate && $this->status == "Unknown"){
            $this->setStatus('Too Late');
        }


        if (key_exists($this->status, $this->statusList)) {
            return $this->statusList[$this->status];
        }
        return $this->statusList['Unknown'];
    }

    public function getRole()
    {
        if (key_exists($this->role, $this->roleList)) {
            return $this->roleList[$this->role];
        }
        return $this->roleList['Unknown'];
    }

    public function setStatus($status)
    {
        if (key_exists($status, $this->statusList)) {
            $this->status = $status;
            return true;
        }
        return false;
    }

    public function setRole($role)
    {
        if (key_exists($role, $this->roleList)) {
            $this->role = $role;
            return true;
        }
        return false;
    }

    public function updateAPI(Character $second_char = null)
    {
        if ($second_char->ID) {
            return $this->api->changeSignup($this->raid, $this->character, $this->status, $this->role, $this->comment, $second_char);
        } else {
            return $this->api->changeSignup($this->raid, $this->character, $this->status, $this->role, $this->comment);
        }
    }
}