<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CalendarGame extends Model
{

    protected $fillable = [
        'calendar_id',
        'home_team_id',
        'away_team_id',
        'date',
        'day',
        'type',
        'home_goals',
        'away_goals',
        'home_points',
        'away_points',
        'played'
    ];

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

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

    public function getHomeTeam()
    {

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

        if ($team_id == null)
        {
            $t = new Team();
            $t->name = 'Riposo';
        }
        else
        {
            $t = Team::findOrFail($team_id); // $this->belongsTo('App\Team');

            if (($t->type != '' && $t->type != '-') || $t->excluded)
            {
                if ($t->type == 'andata')
                {
                    // $t->name = 'Riposo';
                }
                
            }
            
        }
        return $t;
    }

    public function getAwayTeam()
    {
        $team_id =  $this->away_team_id;
        //if ($this->type == 'RITORNO')
        //    $team_id =  $this->home_team_id;

        if ($team_id == null)
        {
            $t = new Team();
            $t->name = 'Riposo';
        }
        else
        {
            $t = Team::findOrFail($team_id); // $this->belongsTo('App\Team');
            if (($t->type != '' && $t->type != '-') || $t->excluded)
            {
                if ($t->type == 'andata')
                {
                    // $t->name = 'Riposo';
                }
            }
            /*
            if ($t->excluded)
            {
                $t->name = 'Riposo';
            }
            else
            {
                if (strtoupper($t->type) == 'ANDATA')
                {
                    if ($this->type == 'ANDATA')
                    {
                        if ($this->day > $t->day)
                        {
                            $t->name = 'Riposo';
                        }
                    }
                }
            }*/
        }
        return $t;
    }

    public function getResult()
    {

        $res = '';
        //if ($this->played)
        //{
            if($this->away_team_id != null && $this->home_team_id != null)
            {

                // Controllo se una squadra è stata esclusa
                $exc = false;
                $t = Team::findOrFail($this->home_team_id);
                if (strtoupper($t->type) == 'ANDATA') // && $t->type != '-') || $t->excluded)
                    $exc = true;
                $t = Team::findOrFail($this->away_team_id);
                if (strtoupper($t->type) == 'ANDATA') // && $t->type != '-') || $t->excluded)
                    $exc = true;
                if ($exc)
                    $res = "NV";
                else
                    $res = $this->home_goals . " - " . $this->away_goals;

            }
        //}
        //$res = $this->home_goals . " - " . $this->away_goals;
        return $res;

    }      

}