# Libraries
import altair as alt
import pandas as pd
import re
import regex
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mcolors
import matplotlib.cm
import matplotlib.font_manager as fm

# -*- coding: utf-8 -*-
"""Matplotlib_02.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1OduaKQslyxiCeuI5shKfxlPfNl7IZqPx
"""
path = r'sentiport/utils/Helvetica-Font/Helvetica-Bold.ttf'
fontprop = fm.FontProperties(fname=path, size=15)


# @title User Rating Plot Function as "plot_matplotlib_totalrating_3(dataframe)"
def plot_overall_rating(dataframe, temp_dir):
    '''
    This function is used to plot total review by version

    Parameters:
    dataframe-- data from get_crawl_data (but we can put data from apple scrapper if the columns names are identical)
    
    returns:
    plot
    '''
    # Overal Rating Chart
    # Preprocessing
    df = dataframe
    grouped_multiple = df.groupby(['rating']).agg({'review': ['count']})
    grouped_multiple.columns = ['review']
    grouped_multiple = grouped_multiple.reset_index()
    grouped_multiple.sort_values(by=['rating', 'review'], inplace=False, ascending=False)
    grouped_multiple['value'] = (grouped_multiple['review'] / grouped_multiple['review'].sum()) * 100
    grouped_multiple['value'] = grouped_multiple['value'].astype(int)

    # Plot
    value = grouped_multiple['value']
    bars = grouped_multiple['rating']
    cmap = mcolors.LinearSegmentedColormap.from_list("", ["#e0432f", "#44bb55"])

    # Create horizontal bars
    plt.figure(figsize=(3.8377, 2.9858), dpi=100)
    plt.barh(bars, value, color=cmap(grouped_multiple.rating.values / grouped_multiple.rating.values.max()))

    # Create names on the y-axis
    plt.yticks(bars)
    plt.title("Total Rating", fontproperties=fontprop)
    plt.xlabel("%")
    plt.xlim(xmin=0)
    plt.ylim(ymin=0)
    plt.grid(False)

    plt.savefig(f"sentiport/artifacts/{temp_dir}/fig_overall_rating.png",
                bbox_inches='tight')
    return f"sentiport/artifacts/{temp_dir}/fig_overall_rating.png"
