import pandas as pd
import seaborn as sns
import numpy as np
import os
import matplotlib.pyplot as plt
def pipeLine(address):
    #address = "../../data/raw/cars.csv"
    
    #load
    df = pd.read_csv(address,
                     na_values=['No Reasonable offer refused - We Finance', 'NO ACCIDENT, LOCAL, CERTIFIED',
                                'U WORK U DRIVE - CALL NOW !!'])
    #clean
    df1=(
        df.drop(columns=['Unnamed: 0','body text','fuel','latlong','sale type','size'])
        .dropna().reset_index(drop=True).rename(columns={'paint color':'colour','title status':'title'})  
    )
    df1['colour']=df1['colour'].str.strip()
    df1['condition']=df1['condition'].str.strip()
    df1['location']=df1['location'].str.lower()
    
    #process
    df1[['year','make','model']]=df1['make'].str.split(expand=True,n=2)
    df1['price']=df1['price'].replace('\$|,', '', regex=True).str.strip().astype(int)
    df1['odometer']=df1['odometer'].astype(int)
    
    #wrangle
    df2=(
        df1[['year','make','model','price','odometer','condition',
            'location','colour','transmission','title','drive','cylinders','type']]
    )
    
    #return
    return df2
def loadData(address):
    #address = "../../data/raw/cars.csv"
    df = pd.read_csv(address,
                     na_values=['No Reasonable offer refused - We Finance', 'NO ACCIDENT, LOCAL, CERTIFIED',
                                'U WORK U DRIVE - CALL NOW !!'])
    return df
#loadData()
def cleanData(df):
    df1=(
        df.drop(columns=['Unnamed: 0','body text','fuel','latlong','sale type','size'])
        .dropna().reset_index(drop=True).rename(columns={'paint color':'colour','title status':'title'})  
    )
    df1['colour']=df1['colour'].str.strip()
    df1['condition']=df1['condition'].str.strip()
    df1['location']=df1['location'].str.lower()
    return df
#cleanData(loadData(address))
def procData(df):
    df1[['year','make','model']]=df1['make'].str.split(expand=True,n=2)
    df1['price']=df1['price'].replace('\$|,', '', regex=True).str.strip().astype(int)
    df1['odometer']=df1['odometer'].astype(int)
    return df
#.rename({0: 'year', 1: 'make', 2:'model'}, axis=1)
#procData(cleanData(loadData(address)))
def wrangData(df):
    df=(
        df[['year','make','model','price','odometer','condition',
            'location','colour','transmission','title','drive','cylinders','type']]
    )
    return df
#wrangData(procData(cleanData(loadData(address))))
def MM_graph(df):
    col_list=list(sorted(set(df['colour'])))
    df_col = pd.DataFrame(index=col_list, columns=['mean price','mean km','median price','median km'])
    meanp= []
    for col in col_list:
        meanp.append(int((df.loc[df['colour']==col])['price'].mean()))
    df_col['mean price']=meanp
    meank= []
    for col in col_list:
        meank.append(int((df.loc[df['colour']==col])['odometer'].mean()))
    df_col['mean km']=meank
    medianp= []
    for col in col_list:
        medianp.append(int((df.loc[df['colour']==col])['price'].median()))
    df_col['median price']=medianp
    mediank= []
    for col in col_list:
        mediank.append(int((df.loc[df['colour']==col])['odometer'].median()))
    df_col['median km']=mediank
    pg=df_col[['mean price','median price']].plot.bar()
    kg=df_col[['mean km','median km']].plot.bar()
    display(pg,kg)
def col_count(df):
    col_list=list(sorted(set(df['colour'])))
    display(sns.countplot(y="colour",data=df,order=col_list))
def outliers(df):
    col_list=list(sorted(set(df['colour'])))
    display(sns.countplot(y="colour",data=df.loc[df['price']>200000],order=col_list))
def outoutliers(df):
    display(df.loc[df['price']>1000000])
