<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\CalendarGame;

class Calendar extends Model
{

    protected $fillable = [
        'name',
        'section_id',
        'region_id',
        'season_id',
        'category_id',
        'group_id',
        'type',
        'position',
        'archived'
    ];

    public function season()
    {
        return $this->belongsTo('App\Season');
    }

    public function section()
    {
        return $this->belongsTo('App\Section');
    }

    public function region()
    {
        return $this->belongsTo('App\Section');
    }

    public function category()
    {
        return $this->belongsTo('App\Category');
    }

    public function group()
    {
        return $this->belongsTo('App\Group');
    }

    public function getRates()
    {

        /*
        Penalizzazioni

        Se mi ritiro prima del girone di ritorno
        Mi ritiro alla 10
        Dall’11 in poi scrivo riposo

        Gestione punti penalizzazione rinunce

        Se dal ritorno, quelle prima valide, quelle dopo il ritiro 0-6 tavolino

        Se è stata esclusa in classifica tutto a 0

        */

        $aRate = array();
        // Carico le squadre
        $games = CalendarGame::where('calendar_id', '=', $this->id)->get();
        foreach($games as $g)
        {

            $home_team_id =  $g->home_team_id;
            //if ($g->type == 'RITORNO')
            //    $home_team_id =  $g->away_team_id;

            $away_team_id =  $g->away_team_id;
            //if ($g->type == 'RITORNO')
            //    $homeaway_team_id =  $g->home_team_id;

            if ($home_team_id != null && $away_team_id != null)
            {

                if (!isset($aRate[$home_team_id]))
                {
                    $t = Team::findOrFail($home_team_id);
                    $points = 0;
                    if ($t->penality > 0)
                        $points -= $t->penality;
                    $penality = '';
                    $order = 0;
                    if ($t->excluded)
                    {
                        $penality .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $order = -999;
                    }
                    if ($t->type != '' && $t->type != '-' && !$t->excluded)
                    {
                        $penality .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $order = -999;
                    }
                    if ($t->penality > 0 && $penality == '')
                    {
                        $penality .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
                        // $order = -555;
                    }
                    $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $penality, 'order' => $order);
                }

                if (!isset($aRate[$away_team_id]))
                {
                    $t = Team::findOrFail($away_team_id);
                    $points = 0;
                    if ($t->penality > 0)
                        $points -= $t->penality;
                    $penality = '';
                    $order = 0;
                    if ($t->excluded)
                    {
                        $penality .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $order = -999;
                    }
                    if ($t->type != '' && $t->type != '-' && !$t->excluded)
                    {
                        $penality .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $order = -999;
                    }
                    if ($t->penality > 0 && $penality == '')
                    {
                        $penality .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
                        // $order = -555;
                    }
                    $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $penality, 'order' => $order);
                }

                if ($home_team_id != NULL && $away_team_id != NULL && $g->played)
                {
                    if (!isset($aRate[$home_team_id]))
                    {
                        $t = Team::findOrFail($home_team_id);
                        $points = 0;
                        if ($t->penality > 0)
                            $points -= $t->penality;
                        $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $order);
                    }

                    if (!isset($aRate[$away_team_id]))
                    {
                        $t = Team::findOrFail($away_team_id);
                        $points = 0;
                        if ($t->penality > 0)
                            $points -= $t->penality;
                        $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => $points, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $order);
                    }

                    // Controllo la squadra di casa
                    $t = Team::findOrFail($home_team_id);

                    $p_home = '';
                    $home_order = 0;
                    $home_pen = false;
                    $home_no_game = false;
                    if ($t->excluded)
                    {
                        $home_pen = true;
                        $home_order = -999;
                        $p_home .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $home_order, 'penality' => $p_home);
                    }
                    if ($t->type != '' && $t->type != '-' && !$t->excluded)
                    {
                        $home_pen = true;
                        $home_order = -999;
                        $p_home .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $aRate[$home_team_id] = array('team' => $g->homeTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $home_order, 'penality' => $p_home);
                    }
                    if ($t->penality > 0 && $p_home == '')
                    {
                        $p_home .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
                        //$home_order = -555;
                    }


                    $t = Team::findOrFail($away_team_id);
                    $p_away = '';
                    $away_order = 0;
                    $away_pen = false;
                    $away_no_game = false;
                    if ($t->excluded)
                    {
                        $away_pen = true;
                        $away_order = -999;
                        $p_away .= 'Esclusa dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $away_order, 'penality' => $p_away);
                    }
                    if ($t->type != '' && $t->type != '-' && !$t->excluded)
                    {
                        $away_pen = true;
                        $away_order = -999;
                        $p_away .= 'Ritirata dal campionato';// dalla ' . $t->day . ' giornata di ' . $t->type;
                        $aRate[$away_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $away_order, 'penality' => $p_away);
                    }
                    if ($t->penality > 0 && $p_away == '')
                    {
                        $p_away .= $t->penality . ' punt' . ($t->penality > 1 ? 'i' : 'o') . ' di penalizzazione';
                        // $away_order = -555;
                    }

                    // HOME
                    $t = Team::findOrFail($home_team_id);
                    if (($t->type != '' && $t->type != '-') || $t->excluded)
                    {
                        if ($t->type == 'andata')
                        {
                            $home_no_game = true;
                            $g->home_goals = 0;
                            $g->home_points = 0;
                            $g->away_goals = 0;
                            $g->away_points = 0;
                            /*
                            if ($g->type == 'ANDATA')
                            {
                                if ($g->day > $t->day)
                                {
                                    $g->home_goals = 0;
                                    $g->home_points = 0;
                                }
                            }
                            if ($g->type == 'RITORNO')
                            {
                                $g->home_goals = 0;
                                $g->home_points = 0;
                            }
                            */
                        }
                        if ($t->type == 'ritorno')
                        {
                            if ($g->type == 'RITORNO')
                            {
                                if ($g->day > $t->day)
                                {
                                    $g->home_goals = 0;
                                    $g->home_points = 0;
                                    $g->away_goals = 6;
                                    $g->away_points = 3;
                                }
                            }
                        }
                    }

                    // AWAY
                    $t = Team::findOrFail($away_team_id);
                    if (($t->type != '' && $t->type != '-') || $t->excluded)
                    {
                        if ($t->type == 'andata')
                        {
                            $away_no_game = true;
                            $g->away_goals = 0;
                            $g->away_points = 0;
                            $g->home_goals = 0;
                            $g->home_points = 0;
                            /*if ($g->type == 'ANDATA')
                            {
                                if ($g->day > $t->day)
                                {
                                    $g->away_goals = 0;
                                    $g->away_points = 0;
                                }
                            }
                            if ($g->type == 'RITORNO')
                            {
                                $g->away_goals = 0;
                                $g->away_points = 0;
                            }*/
                        }
                        if ($t->type == 'ritorno')
                        {
                            if ($g->type == 'RITORNO')
                            {
                                if ($g->day > $t->day)
                                {
                                    $g->away_goals = 0;
                                    $g->away_points = 0;
                                    $g->home_goals = 6;
                                    $g->home_points = 3;
                                }
                            }
                        }
                    }

                    $c_home = $aRate[$home_team_id];
                    $POINTS = $c_home["POINTS"] + $g->home_points;
                    if ($home_order == -999)
                        $aRate[$home_team_id] = array('team' => $g->awayTeam->name, 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'order' => $away_order, 'penality' => $p_home);

                    if ($g->played && !$away_no_game)
                    {
                        $PG = $c_home["PG"] + ($g->played ? 1 : 0);
                        $V = $c_home["V"] + ($g->home_goals > $g->away_goals ? 1 : 0);
                        $N = $c_home["N"] + ($g->home_goals == $g->away_goals ? 1 : 0);
                        $P = $c_home["P"] + ($g->home_goals < $g->away_goals ? 1 : 0);
                        $GF = $c_home["GF"] + $g->home_goals;
                        $GS = $c_home["GS"] + $g->away_goals;
                        $DR = $GF - $GS;
                    }
                    else
                    {
                        $PG = $c_home["PG"];
                        $V = $c_home["V"];
                        $N = $c_home["N"];
                        $P = $c_home["P"];
                        $GF = $c_home["GF"];
                        $GS = $c_home["GS"];
                        $DR = $GF - $GS;
                    }

                    if (!$home_pen)
                        $aRate[$home_team_id] = array('team' => $c_home["team"], 'POINTS' => $POINTS, 'PG' => $PG, 'V' => $V, 'N' => $N, 'P' => $P, 'GF' => $GF, 'GS' => $GS, 'DR' => $DR, 'penality' => $p_home, 'order' => $home_order);
                    else
                        $aRate[$home_team_id] = array('team' => $c_home["team"], 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $p_home, 'order' => $home_order);

                    $c_away = $aRate[$away_team_id];
                    $POINTS = $c_away["POINTS"] + $g->away_points;
                    if ($away_order == -999)
                        $POINTS = 0;

                    if ($g->played && !$home_no_game)
                    {
                        $PG = $c_away["PG"] + ($g->played ? 1 : 0);
                        $V = $c_away["V"] + ($g->away_goals > $g->home_goals ? 1 : 0);
                        $N = $c_away["N"] + ($g->away_goals == $g->home_goals ? 1 : 0);
                        $P = $c_away["P"] + ($g->away_goals < $g->home_goals ? 1 : 0);
                        $GF = $c_away["GF"] + $g->away_goals;
                        $GS = $c_away["GS"] + $g->home_goals;
                        $DR = $GF - $GS;
                    }
                    else
                    {
                        $PG = $c_away["PG"];
                        $V = $c_away["V"];
                        $N = $c_away["N"];
                        $P = $c_away["P"];
                        $GF = $c_away["GF"];
                        $GS = $c_away["GS"];
                        $DR = $GF - $GS;
                    }

                    if (!$away_pen)
                        $aRate[$away_team_id] = array('team' => $c_away["team"],  'POINTS' => $POINTS,'PG' => $PG, 'V' => $V, 'N' => $N, 'P' => $P, 'GF' => $GF, 'GS' => $GS, 'DR' => $DR, 'penality' => $p_away, 'order' => $away_order);
                    else
                        $aRate[$away_team_id] = array('team' => $c_away["team"], 'POINTS' => 0, 'PG' => 0, 'V' => 0, 'N' => 0, 'P' => 0, 'GF' => 0, 'GS' => 0, 'DR' => 0, 'penality' => $p_away, 'order' => $away_order);


                }

            }

        }

        $sorts = array('order' => 'desc', 'POINTS' => 'desc', 'DR' => 'desc', 'GF' => 'desc');

        usort($aRate, function($a, $b) use ($sorts) {
            foreach($sorts as $field => $direction) {
                if ($a[$field] != $b[$field]) {
                    if ($direction == 'asc') {
                        return $a[$field] < $b[$field] ? -1 : 1;
                    }
                    return $a[$field] < $b[$field] ? 1 : -1;
                }
            }
            return 0;
        });

        return $aRate;

    }

    public function by_points($a, $b)
    {
        return $a['POINTS'] > $b['POINTS'];
    }

}
