import matplotlib.pyplot as plt
import pandas as pd


def group_by_category(df):
    grouped = df.groupby(['CATEGORY']).size().to_frame('Crimes')
    labels = ['Trespassing', 'Vehicle theft', 'General Theft',
              'Damage to Property', 'Robbery', 'Homicide']
    p = grouped.plot.pie(y='Crimes', labels=labels, autopct='%1.1f%%')
    p.set_title('Crimes Percentage Grouped By Category')
    p.get_legend().remove()
    plt.savefig('../charts/category.png')

def group_by_time_of_day(df):
    grouped = df.groupby(['TIME_OF_DAY']).size().to_frame('Crimes')
    p = grouped.plot.pie(y='Crimes', labels=['Day', 'Evening', 'Night'], autopct='%1.1f%%')
    p.set_title('Crimes Percentage Grouped By Time of Day')
    p.get_legend().remove()
    plt.savefig('../charts/time_of_day.png')

def group_by_day_of_the_week(df):
    grouped = df.groupby(['DAY_OF_THE_WEEK']).size().to_frame('Crimes')
    labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    p = grouped.plot.pie(y='Crimes', labels=labels, autopct='%1.1f%%')
    p.set_title('Crimes Percentage Grouped By Day of The Week')
    p.get_legend().remove()
    plt.savefig('../charts/day_of_the_week.png')

def group_by_month(df):
    grouped = df.groupby(['MONTH']).size().to_frame('Size')
    grouped['Percentage'] = 100 * grouped['Size'] / len(df)
    grouped = grouped.drop(columns='Size')
    p = grouped.plot.bar()
    p.set_title('Crimes Percentage Grouped By Month')
    p.set_ylabel('Percentage of Crimes')
    p.set_xlabel('Month')
    p.get_legend().remove()
    plt.savefig('../charts/month.png')

def group_by_year(df):
    grouped = df.groupby(['YEAR']).size().to_frame('Crimes')
    p = grouped.plot.pie(y='Crimes', autopct='%1.1f%%')
    p.set_title('Crimes Percentage Grouped By Year')
    p.get_legend().remove()
    plt.savefig('../charts/year.png')

def group_by_territory(df):
    grouped = df.groupby(['PDQ']).size().to_frame('Size')
    grouped['Percentage'] = 100 * grouped['Size'] / len(df)
    grouped = grouped.drop(columns='Size')
    grouped.index = grouped.index.astype(int)
    p = grouped.plot.bar()
    p.set_title('Crimes Percentage Grouped By Territory')
    p.set_ylabel('Percentage of Crimes')
    p.set_xlabel('Territory Number')
    p.get_legend().remove()
    plt.savefig('../charts/territory.png')


if __name__ == '__main__':
    df = pd.read_csv('../data/crimes_dataset_processed_incomplete.csv')
    group_by_territory(df)
    group_by_year(df)
    group_by_month(df)
    group_by_time_of_day(df)
    group_by_day_of_the_week(df)
    group_by_category(df)

[((12, 4, 12, 41), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(12, 16, 12, 40): '"""../charts/category.png"""'}, {}), "('../charts/category.png')", True, 'import matplotlib.pyplot as plt\n'), ((19, 4, 19, 44), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(19, 16, 19, 43): '"""../charts/time_of_day.png"""'}, {}), "('../charts/time_of_day.png')", True, 'import matplotlib.pyplot as plt\n'), ((27, 4, 27, 48), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(27, 16, 27, 47): '"""../charts/day_of_the_week.png"""'}, {}), "('../charts/day_of_the_week.png')", True, 'import matplotlib.pyplot as plt\n'), ((38, 4, 38, 38), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(38, 16, 38, 37): '"""../charts/month.png"""'}, {}), "('../charts/month.png')", True, 'import matplotlib.pyplot as plt\n'), ((45, 4, 45, 37), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(45, 16, 45, 36): '"""../charts/year.png"""'}, {}), "('../charts/year.png')", True, 'import matplotlib.pyplot as plt\n'), ((57, 4, 57, 42), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(57, 16, 57, 41): '"""../charts/territory.png"""'}, {}), "('../charts/territory.png')", True, 'import matplotlib.pyplot as plt\n'), ((61, 9, 61, 71), 'pandas.read_csv', 'pd.read_csv', ({(61, 21, 61, 70): '"""../data/crimes_dataset_processed_incomplete.csv"""'}, {}), "('../data/crimes_dataset_processed_incomplete.csv')", True, 'import pandas as pd\n')]