import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
from matplotlib.patches import Circle, Rectangle, Arc
import math
import random

X_SIZE = 315
Y_SIZE = 204

BOX_HEIGHT = 120.96 
BOX_WIDTH = 49.5

GOAL = 7.32*100/Y_SIZE
GOAL_AREA_HEIGHT = (18.32 * Y_SIZE) / 68
GOAL_AREA_WIDTH = (5.5 * X_SIZE) / 105

def draw_pitch():
    fig = plt.figure(figsize=(105/15, 68/15))
    fig.patch.set_facecolor('#a8bc95')

    axes = fig.add_subplot(1, 1, 1, facecolor='#a8bc95')

    axes.xaxis.set_visible(False)
    axes.yaxis.set_visible(False)

    axes.set_xlim(-157.5,157.5)
    axes.set_ylim(-102,102)  

    axes = draw_patches(axes)
    
    return fig, axes

def draw_patches(axes):
    plt.xlim([-161.5,161.5])
    plt.ylim([-104,104])
    plt.gca().invert_yaxis()
    
    #Draw the pitch outlines 
    axes.add_patch(plt.Rectangle((-157.5, -102), 315, 204,
                       edgecolor="white", facecolor="none", alpha=1))
    
    #Draw half-way line
    axes.add_line(plt.Line2D([0, 0], [-102, 102],
                    c='w'))
    
    # halfway circle 
    axes.add_patch(Ellipse((0,0), 2*(9.15*X_SIZE)/105, 2*(9.15*Y_SIZE)/68, ec='w', fc='none'))
    
    # Box area
    axes.add_patch(plt.Rectangle((157.5-BOX_WIDTH, -102+41.52), BOX_WIDTH, BOX_HEIGHT, ec='w', fc='none'))
    axes.add_patch(plt.Rectangle((-157.5, -102+41.52), BOX_WIDTH, BOX_HEIGHT, ec='w', fc='none'))
    
    #goal
    axes.add_patch(plt.Rectangle((157.5-GOAL_AREA_WIDTH, -102+74.52),  GOAL_AREA_WIDTH, GOAL_AREA_HEIGHT,
                       ec='w', fc='none'))
    axes.add_patch(plt.Rectangle((-157.5, -102+74.52),  GOAL_AREA_WIDTH, GOAL_AREA_HEIGHT,
                       ec='w', fc='none'))
    
    return axes