import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas_profiling
from pandas import Series, DataFrame
from scipy.stats import norm

## Reflection Question: Comparison between owner sales and dealer sales.
## lOADING AND CLEANING DATA:

df = pd.read_csv('/Users/harjyotsingh/UBC/YEAR3/Summer/data301/project-group5/data/raw/cars.csv')
df[['year','make','model']]=df['make'].str.split(expand=True,n=2)
df = df.drop(['body text','Unnamed: 0'],axis=1) 

df.dropna(how='all',subset=["location","condition","cylinders"])
    

df = df.dropna()


df = df[~pd.isna(df["condition"])] 
df["location"] = df.location.str.lower()


badValues = pd.Series(['No Reasonable offer refused - We Finance', 'NO ACCIDENT, LOCAL, CERTIFIED',
             'U WORK U DRIVE - CALL NOW !!','you work you drive']).str.lower()

df = df[~df.isin({'location':badValues.values})]


df = df[~pd.isna(df["location"])]

df = df[~pd.isna(df["cylinders"])]

df = df[~pd.isna(df["drive"])]

df['location'] = np.where(df['location'].str.lower().str.startswith('vancouver'), 'vancouver', df['location'])

df.cylinders = df['cylinders'].str.split(' ').str[1]

df['price']=df['price'].replace('\$|,', '', regex=True).str.strip().astype(int)
df

owner_df = df.loc[df['sale type'] == "owner"] 
dealer_df = df.loc[df['sale type'] == "dealer"] 

##DATA Visualization:

sns.set_theme(style="white",
              font_scale=1.3, 
             )

sns.catplot(data=df, kind="bar", x="sale type", y="price")
plt.title('Mean price difference between two sale types')
plt.xlabel('sale type')
plt.ylabel('price')

sns.catplot(data=df, kind="bar", x="sale type", y="odometer")
plt.title('Average odometer reading difference between two sale types')
plt.xlabel('sale type')
plt.ylabel('odometer')

owner_eData = {}
dealer_eData = {}

for area in (owner_df["location"].unique()):
    percentage = ((owner_df[owner_df["location"]==area]["condition"].where(owner_df["condition"] == " excellent").count()/
                owner_df[owner_df["location"]==area]["condition"].count()))
    count = owner_df[owner_df["location"]==area]["condition"].count()
    if percentage not in [0.0,1.0] and count > 500:
        owner_eData[area] = percentage

for area in (dealer_df["location"].unique()):
    percentage = ((dealer_df[dealer_df["location"]==area]["condition"].where(dealer_df["condition"] == " excellent").count()/
                dealer_df[dealer_df["location"]==area]["condition"].count()))
    count = dealer_df[dealer_df["location"]==area]["condition"].count()
    if percentage not in [0.0,1.0] and count > 500:
        dealer_eData[area] = percentage
        
        dict(sorted(owner_eData.items(), key=lambda item: item[1],reverse=False))

plt.show()

f = plt.figure()
f.set_figwidth(10)
f.set_figheight(10)

plt.yticks(fontsize=10)
plt.bar(range(len(owner_eData)), list(owner_eData.values()), align='center')
plt.xticks(range(len(owner_eData)), list(owner_eData.keys()),fontsize=2)

dict(sorted(dealer_eData.items(), key=lambda item: item[1],reverse=False))

plt.show()

f = plt.figure()
f.set_figwidth(30)
f.set_figheight(30)

plt.yticks(fontsize=30)
plt.bar(range(len(dealer_eData)), list(dealer_eData.values()), align='center')
plt.xticks(range(len(dealer_eData)), list(dealer_eData.keys()),fontsize=15)