import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'c:/users/pptallon/appdata/local/Anaconda3/Library/plugins/platforms'
import pandas as pd
import numpy as np
import wget
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.colors as mcolors
import folium
import matplotlib.patches as mpatches
from matplotlib.ticker import FuncFormatter
import json
import plotly.express as px
import geopandas as gpd
import plotly.graph_objects as go
path = "C:/Users/pptallon/Desktop/"

filename = "Listings.csv"

df1 = pd.read_csv(path + filename, encoding='utf-8-sig')

# Currency Conversion

rate = 0.0068
df1['price'] = df1['price'].str.replace(',', '')
df1['price'] = df1['price'].str.replace('$', '')
df1['price'] = pd.to_numeric(df1['price'])
amount = df1['price']

df1['priceusd'] = rate * amount
with open(r'C:\Users\pptallon\Desktop\tokyoneighbourhoods.geojson', 'r', encoding='utf-8') as f:
    tokyo_geo = json.load(f)

# Group by neighborhood and calculate mean price
neigh_df = df1.groupby('neighbourhood_cleansed')['priceusd'].mean().reset_index()

# Apply log transformation
neigh_df['log_priceusd'] = np.log1p(neigh_df['priceusd'])

# Create the Mapbox choropleth
fig = px.choropleth_mapbox(
    neigh_df,
    geojson=tokyo_geo,
    locations='neighbourhood_cleansed',  
    featureidkey='properties.neighbourhood',  
    color='log_priceusd',  
    color_continuous_scale=['green', 'yellow', 'red'],  
    hover_data={'neighbourhood_cleansed': True, 'priceusd': ':.2f'},  # Format price with 2 decimals
    title="Tokyo Airbnb Prices by Neighborhood (Log Scale)",
    labels={'log_priceusd': 'Log(Price + 1)'},
    mapbox_style="carto-positron",  # Change to Carto-Positron style
    center={"lat": 35.682855, "lon": 139.417413},  # Center on Tokyo
    zoom=9,  # Adjust zoom level for better visibility
    width = 1000,
    height = 800
)

# Define log-scale range
log_min, log_max = np.log1p(neigh_df['priceusd'].min()), np.log1p(neigh_df['priceusd'].max())

# Define tick positions (log values) and corresponding price labels with comma separators
tick_positions = np.linspace(log_min, log_max, num=5)
tick_labels = [f"${format(int(np.expm1(v)), ',')}" for v in tick_positions]  # Add commas

fig.update_layout(
    coloraxis_colorbar=dict(
        title="Price (USD)",  
        tickvals=tick_positions,  
        ticktext=tick_labels,  # Convert log values back to readable price labels with commas
    )
);

fig.show()