from bookstack import Bookstack
from update_films_by_country import get_films_by_country, build_page as build_films_by_country_page, FILMS_BY_COUNTRY_PAGE_ID
from update_films_by_year import get_films_by_year, build_page as build_films_by_year_page, FILMS_BY_YEAR_PAGE_ID
from vcinema_utils import VCinemaUtils
from update_viewings_csv import update_viewings_csv

import argparse
from progress.bar import IncrementalBar
import plotly.express as px
import pandas
import geopandas
import numpy as np

from urllib.request import urlopen
import json


country_alpha3 = {}



def plot_countries(token_id, token_secret):
    df = pandas.read_csv('country_count.csv', header=0)

    url = 'https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson'
    with urlopen(url) as response:
        countries = json.load(response)


    print(countries['features'][0]['properties'].keys())

    fig = px.choropleth_mapbox(df,geojson=countries,locations=df['Alpha'],color=np.log10(df['Count']),hover_name=df['Country'],hover_data=[df['Count']],color_continuous_scale=px.colors.sequential.YlGn, featureidkey="properties.ISO_A3",mapbox_style="stamen-terrain",opacity=0.5)
    fig.update_geos(fitbounds="locations")
    fig.update_traces(hovertemplate="<b>%{hovertext}</b><br>Count: %{customdata}")
    fig.update_layout(coloraxis_showscale=False)

    fig.show()
#

def test():
    from urllib.request import urlopen
    import json
    with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
        counties = json.load(response)

    import pandas as pd
    df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
                     dtype={"fips": str})

    import plotly.graph_objects as go

    print("DF")

    print(df)

    fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=df.fips, z=df.unemp,
                                        colorscale="Viridis", zmin=0, zmax=12,
                                        marker_opacity=0.5, marker_line_width=0))
    fig.update_layout(mapbox_style="carto-positron",
                      mapbox_zoom=3, mapbox_center={"lat": 37.0902, "lon": -95.7129})
    fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
    fig.show()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Update wiki pages.')
    parser.add_argument('token_id', help='API token ID.')
    parser.add_argument('token_secret', help='API token secret.')

    args = parser.parse_args()

    # test()

    plot_countries(args.token_id, args.token_secret)

# pycountry.countries.get(name='American Samoa').alpha2
