#!/usr/bin/python3

import random
from humans import Human
from cities import City
from clubs import Club

class Match():

    def __init__(self, teamHome, teamAway):
        self.teamHome = teamHome
        self.teamAway = teamAway
        self.homeScore = 0
        self.awayScore = 0
        self.result = 0 # 0 = unplayed

    def sim_match(self):
        homeTier = 0.0
        awayTier = 0.0

        # add player tiers
        for player in self.teamHome.players:
            homeTier += player.tier

        for player in self.teamAway.players:
            awayTier += player.tier

        homeTier = homeTier*random.uniform(0,2)
        awayTier = awayTier*random.uniform(0,2)

        # do some randoms.. more chances to score based on your tier
        # you must have a higher end result than the opponent for to score a goal
        for goalAtt in range(0, 5):
                home = random.randint(0, 10) + homeTier
                away = random.randint(0, 8) + awayTier
                if home > away:
                    self.homeScore += 1

        for goalAtt in range(0, 4):
                home = random.randint(0, 8) + homeTier
                away = random.randint(0, 10) + awayTier
                if away > home:
                    self.awayScore += 1

        # 1: home win -- 2: away win -- 3: draw
        if self.homeScore > self.awayScore:
            self.result = 1
        elif self.awayScore > self.homeScore:
            self.result = 2
        else:
            self.result = 3

    def print_postmatch(self):
        print(" Home: {homeScore} :{teamHome}\n Away: {awayScore} :{teamAway}\n".format(homeScore=self.homeScore, teamHome=self.teamHome.name, awayScore=self.awayScore, teamAway=self.teamAway.name))
            
    def print_prematch(self):
        print(" HOME: {0}\n AWAY: {1}\n".format(self.teamHome.name, self.teamAway.name))
