<?php

namespace WoWPress\Models;

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

class SKS extends Model
{

    protected $table = "wowpress_sks";
    public $timestamps = false;
    protected $primaryKey = 'ID';
    protected $guarded = ['ID'];



    public static function getList($name)
    {
        return static::where('list_name', $name)->orderBy('rank')->get();
    }



    public static function getLists()
    {
        return static::groupBy('list_name')->get();
    }

    public function getCharacterAttribute()
    {
        $char = Character::where('name', $this->char_name)->where('realm', $this->realm_name)->first();
        if (!empty($char->ID)) {
            return $char;
        }


        $char = new Character();
        $char->name = $this->char_name;
        $char->realm = $this->realm_name;
        $char->class = "yellow";

        return $char;
    }

    public function getAfter($active = true)
    {
        $list = static::where('list_name', $this->list_name)->where('rank', ">", $this->rank);
        if ($active) {
            $list = $list->where('active', true);
        }
        return $list->orderBy('rank')->get();
    }

    public function getPrevious($active = true)
    {
        $prev =  SKS::where('list_name', $this->list_name)->where('rank', '<', $this->rank);
        if ($active) {
            $prev =  $prev->where('active', true);
        }
        $prev = $prev->orderBy('rank', 'DESC')->first();

        if (!empty($prev->ID)) {
            return $prev->rank;
        } else {
            return $this->rank;
        }
    }

    public function getLastRankAttribute()
    {
        $lastSK = SKS::where('list_name', $this->list_name)
            ->where('active', true)
            ->orderBy('rank', 'DESC')->get()->first();

        if (!empty($lastSK->ID)) {
            return $lastSK->rank;
        }
        return false;
    }

    public function roll()
    {
        $list = SKS::where('list_name', $this->list_name)->get();
        $ranks = range(1, count($list));
        shuffle($ranks);
        Log::write(json_encode($list->map->only(['rank','char_name','realm_name'])->values()),Log::LOG_ROLL);
        foreach ($list as $key => $sk) {
            $sk->rank = $ranks[$key];
            $sk->save();
        }
    }

    public function loot()
    {
        $after = $this->getAfter();
        $rank = $this->rank;
        $last_rank = $this->last_rank;
        foreach ($after as $id => &$sk) {
            /**
             * Ignoriere inaktive Charaktere & Charaktere 
             * die auf einem höheren Rang als der Looter sind
             * und überspringe die restlichen Anweisungen
             */
            if (!$sk->active || $sk->rank < $this->rank) {
                continue;
            }

            /**
             * Setze den Rang des Charakters auf den Rang des vorherigen aktiven
             */
            $after[$id]->rank = $sk->getPrevious(true);
        }

        /**
         * Setze den Rang des Looters auf den letzen aktiven Rang
         */
        $this->rank = $last_rank;
        $this->save();

        Log::write(json_encode(['char' => $this->char_name."-".$this->realm_name,'rank' => $this->rank,'previous' => $rank,'list' => $this->list_name]),Log::LOG_LOOT);


        foreach ($after as $sk) {
            $sk->save();
        }
    }

    
}