import streamlit as st
import pandas as pd
import numpy as np
import json
import sys
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
import math


COLOR = "black"
BACKGROUND_COLOR = "#fff"


def main():
    load_pages()
    select_block_container_style()


def load_pages():
    # Race 1 Data
    df_r1M = load_data_session('r1M')
    df_r1P1 = load_data_session('r1P1')
    df_r1P2 = load_data_session('r1P2')
    df_r1P3 = load_data_session('r1P3')
    # Race 2 Data
    df_r2M = load_data_session('r2M')
    df_r2P1 = load_data_session('r2P1')
    df_r2P2 = load_data_session('r2P2')
    df_r2P3 = load_data_session('r2P3')
    # Race 3 Data
    df_r3M = load_data_session('r3M')
    df_r3P1 = load_data_session('r3P1')
    df_r3P2 = load_data_session('r3P2')
    df_r3P3 = load_data_session('r3P3')
    # Race 4 Data
    df_r4M = load_data_session('r4M')
    df_r4P1 = load_data_session('r4P1')
    df_r4P2 = load_data_session('r4P2')
    df_r4P3 = load_data_session('r4P3')
    # Race 5 Data
    df_r5M = load_data_session('r5M')
    df_r5P1 = load_data_session('r5P1')
    df_r5P2 = load_data_session('r5P2')
    df_r5P3 = load_data_session('r5P3')
    # Race 6 Data
    df_r6M = load_data_session('r6M')
    df_r6P1 = load_data_session('r6P1')
    df_r6P2 = load_data_session('r6P2')
    df_r6P3 = load_data_session('r6P3')
    # Race 7 Data
    df_r7M = load_data_session('r7M')
    df_r7P1 = load_data_session('r7P1')
    df_r7P2 = load_data_session('r7P2')
    df_r7P3 = load_data_session('r7P3')
    # Race 8 Data
    df_r8M = load_data_session('r8M')
    df_r8P1 = load_data_session('r8P1')
    df_r8P2 = load_data_session('r8P2')
    df_r8P3 = load_data_session('r8P3')
    # Race 9 Data
    df_r9M = load_data_session('r9M')
    df_r9P1 = load_data_session('r9P1')
    df_r9P2 = load_data_session('r9P2')
    df_r9P3 = load_data_session('r9P3')
    # Race 10 Data
    df_r10M = load_data_session('r10M')
    df_r10P1 = load_data_session('r10P1')
    df_r10P2 = load_data_session('r10P2')
    df_r10P3 = load_data_session('r10P3')
    # Race 11 Data
    df_r11M = load_data_session('r11M')
    df_r11P1 = load_data_session('r11P1')
    df_r11P2 = load_data_session('r11P2')
    df_r11P3 = load_data_session('r11P3')
    # Race 12 Data
    df_r12M = load_data_session('r12M')
    df_r12P1 = load_data_session('r12P1')
    df_r12P2 = load_data_session('r12P2')
    df_r12P3 = load_data_session('r12P3')
    # Race 13 Data
    df_r13M = load_data_session('r13M')
    df_r13P1 = load_data_session('r13P1')
    df_r13P2 = load_data_session('r13P2')
    df_r13P3 = load_data_session('r13P3')
    # Race 14 Data
    df_r14M = load_data_session('r14M')
    df_r14P1 = load_data_session('r14P1')
    df_r14P2 = load_data_session('r14P2')
    df_r14P3 = load_data_session('r14P3')
    # Race 15 Data
    df_r15M = load_data_session('r15M')
    df_r15P1 = load_data_session('r15P1')
    df_r15P2 = load_data_session('r15P2')
    df_r15P3 = load_data_session('r15P3')
    # Race 16 Data
    df_r16M = load_data_session('r16M')
    df_r16P1 = load_data_session('r16P1')
    df_r16P2 = load_data_session('r16P2')
    df_r16P3 = load_data_session('r16P3')
    # Race 17 Data
    df_r17M = load_data_session('r17M')
    df_r17P1 = load_data_session('r17P1')
    df_r17P2 = load_data_session('r17P2')
    df_r17P3 = load_data_session('r17P3')
    
    sidebar_dropdownlist = ['Home Page',"17-Abu Dhabi GP","16-Sakhir GP","15-Bahrain GP","14-Turkish GP","13-Imola GP","12-Portugal GP","11-Germany GP","10-Russian GP","9-Tuscany GP","8-Italian GP","7-Belgium GP",
    "6-Spanish GP","5-70th Anniversary GP",'4-British GP','3-Hungary GP','2-Styria GP','1-Austria GP']
    page = st.sidebar.selectbox("Choose a page", sidebar_dropdownlist)

    if page == 'Home Page':
        st.title('Formula 1 2020 Season')
        st.write("")
        st.write("Here is the interactive timing charts for lap times clocked by drivers of respective team during the Formula 1 2020 season.")
        st.write('')
        st.write("")
        st.text('Select a page in the sidebar')
        st.write("")
        st.write("Note : These charts are best to view in bigger screens")
        st.write("The interactive charts are created using plotly and the devlopment of user interface is done with streamlit nd deployed in heroku")

    elif page == '1-Austria GP':
        # SelectBox
        testdayno = st.selectbox("Select Session",["Practice","Main Race"])
        st.write("On ", testdayno)

        if testdayno == "Practice":
            sessionno = st.radio("Select Practice session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r1P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r1P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r1P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r1P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r1P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r1P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r1M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r1M.describe())                
                load_plot2(df_r1M,0,75,50,130)
                load_plot3(df_r1M,65,71)
        else:
            st.write("Session Data is not available.")

    elif page == '2-Styria GP':
        # SelectBox
        testdayno = st.selectbox("Select Session",["Practice","Main Race"])
        st.write("On ", testdayno)

        if testdayno == "Practice":
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r2P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r2P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r2P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r2P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r2P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r2P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r2M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r2M.describe())
                load_plot2(df_r2M,0,75,60,140)
                load_plot3(df_r2M,65,71)
        else:
            st.write("Session Data is not available.")

    elif page == '3-Hungary GP':
        # SelectBox
        testdayno = st.selectbox("Select Practice Session",["Practice","Main Race"])
        st.write("On ", testdayno)

        if testdayno == "Practice":
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r3P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r3P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r3P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r3P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r3P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r3P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r3M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r3M.describe())
                load_plot2(df_r3M,0,75,60,130)
                load_plot3(df_r3M,75,85)
        else:
            st.write("Session Data is not available.")

    elif page == '4-British GP':
        # SelectBox
        testdayno = st.selectbox("Select Session",["Practice","Main Race"])
        st.write("On ", testdayno)

        if testdayno == "Practice":
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r4P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r4P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r4P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r4P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r4P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r4P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r4M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r4M.describe())
                load_plot2(df_r4M,0,55,60,170)
                load_plot3(df_r4M,85,105)
        else:
            st.write("Session Data is not available.")

    elif page == '5-70th Anniversary GP':
        st.markdown("""# Formula 1 - 70th Anniversary Grand Prix 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("5-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r5P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r5P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r5P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r5P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r5P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r5P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r5M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r5M.describe())
                load_plot2(df_r5M,0,55,60,125)
                load_plot3(df_r5M,85,105)
        else:
            st.write("Session Data is not available.")

    elif page == '6-Spanish GP':
        st.markdown("""# Formula 1 - Spanish GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("6-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r6P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r6P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r6P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r6P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r6P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r6P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r6M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r6M.describe())
                load_plot2(df_r6M,0,70,55,110)
                load_plot3(df_r6M,80,95)
        else:
            st.write("Session Data is not available.")

    elif page == '7-Belgium GP':
        st.markdown("""# Formula 1 - Belgium GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("7-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r7P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r7P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r7P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r7P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r7P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r7P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r7M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r7M.describe())
                load_plot2(df_r7M,0,45,75,190)
                load_plot3(df_r7M,105,125)
        else:
            st.write("Session Data is not available.")

    elif page == '8-Italian GP':
        st.markdown("""# Formula 1 - Italian GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("8-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r8P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r8P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r8P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r8P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r8P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r8P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r8M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r8M.describe())
                load_plot2(df_r8M,0,55,55,180)
                load_plot3(df_r8M,80,95)
        else:
            st.write("Session Data is not available.")

    elif page == '9-Tuscany GP':
        st.markdown("""# Formula 1 - Tuscany GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("9-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r9P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r9P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r9P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r9P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r9P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r9P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r9M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r9M.describe())
                load_plot2(df_r9M,0,59,70,210)
                load_plot3(df_r9M,80,90)
        else:
            st.write("Session Data is not available.")

    elif page == '10-Russian GP':
        st.markdown("""# Formula 1 - Russian GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("10-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r10P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r10P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r10P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r10P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r10P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r10P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r10M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r10M.describe())
                load_plot2(df_r10M,0,53,85,210)
                load_plot3(df_r10M,95,110)
        else:
            st.write("Session Data is not available.")

    elif page == '11-Germany GP':
        st.markdown("""# Formula 1 - Germany GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("11-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r11P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r11P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r11P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r11P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r11P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r11P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r11M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r11M.describe())
                load_plot2(df_r11M,0,65,60,170)
                load_plot3(df_r11M,85,100)
        else:
            st.write("Session Data is not available.")

    elif page == '12-Portugal GP':
        st.markdown("""# Formula 1 - Portugal GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("12-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r12P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r12P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r12P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r12P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r12P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r12P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r12M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r12M.describe())
                load_plot2(df_r12M,0,70,65,125)
                load_plot3(df_r12M,75,90)
        else:
            st.write("Session Data is not available.")

    elif page == '13-Imola GP':
        st.markdown("""# Formula 1 - Emilia Romagna GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])

        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("13-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            sectorno = get_sector()
            if sessionno =="Practice 1":
                if df_r13P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r13P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r13P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r13P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r13P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r13P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r13M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r13M.describe())
                load_plot2(df_r13M,0,65,50,150)
                load_plot3(df_r13M,75,85)
        else:
            st.write("Session Data is not available.")

    elif page == '14-Turkish GP':
        st.markdown("""# Formula 1 - Turkish GP 2020""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])
        sectorno = get_sector()
        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("14-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            if sessionno =="Practice 1":
                if df_r14P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r14P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r14P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r14P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r14P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r14P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r14M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r14M.describe())
                load_toptennracefinsh(df_r14M,sectorno)
                load_plot2(df_r14M,0,60,65,150)
                load_plot3(df_r14M,95,110)
        else:
            st.write("Session Data is not available.")

    elif page == '15-Bahrain GP':
        st.markdown("""# Formula 1 - Bahrain GP 2020 at Sakhir International Circuit""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])
        sectorno = get_sector()
        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("15-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            if sessionno =="Practice 1":
                if df_r15P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r15P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r15P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r15P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r15P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r15P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r15M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r15M.describe())
                load_toptennracefinsh(df_r15M,sectorno)
                load_plot2(df_r15M,0,60,90,190)
                load_plot3(df_r15M,90,105)
        else:
            st.write("Session Data is not available.")

    elif page == '16-Sakhir GP':
        st.markdown("""# Formula 1 - Bahrain GP 2020 at Sakhir Outer Circuit""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])
        sectorno = get_sector()
        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("16-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            if sessionno =="Practice 1":
                if df_r16P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r16P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r16P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r16P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r16P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r16P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r16M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r16M.describe())
                load_toptennracefinsh(df_r16M,sectorno)
                load_plot2(df_r16M,0,90,50,130)
                load_plot3(df_r16M,50,70)
        else:
            st.write("Session Data is not available.")

    elif page == '17-Abu Dhabi GP':
        st.markdown("""# Formula 1 - Abu Dhabi GP 2020 at Yas Marina Circuit""")
        # SelectBox
        testdayno = st.selectbox("Select  Session",["Practice","Main Race"])
        sectorno = get_sector()
        if testdayno == "Practice":
            readme_text = st.markdown(read_markdown("17-Practice.md"))
            sessionno = st.radio("Select Practice Session",("Practice 1","Practice 2","Practice 3"))
            if sessionno =="Practice 1":
                if df_r17P1.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r17P1,True,sectorno)
            elif sessionno =="Practice 2":
                if df_r17P2.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r17P2,True,sectorno)
            elif sessionno =="Practice 3":
                if df_r17P3.empty:
                    st.write("Session Data is not available.")
                else:
                    load_plots(df_r17P3,True,sectorno)
        elif testdayno == "Main Race":
            if df_r17M.empty:
                st.write("Session Data is not available.")
            else:
                #st.write(df_r17M.describe())
                load_toptennracefinsh(df_r17M,sectorno)
                load_plot2(df_r17M,0,60,100,170)
                load_plot3(df_r17M,95,115)
        else:
            st.write("Session Data is not available.")

    else:
        st.text('Select a page in the sidebar')
        
def get_sector():
    sector = st.selectbox("Select Sector",["Overall","Sector 1","Sector 2","Sector 3"])
    #sectorno = "S123"
    if sector == "Overall":
        sectorno = "S123"
    elif sector == "Sector 1":
        sectorno = "S1"
    elif sector == "Sector 2":
        sectorno = "S2"
    elif sector == "Sector 3":
        sectorno = "S3"
    else:
        sectorno = "S123"
    return sectorno


def load_plots(df,ispractice,sectorno):
    #st.write(df.describe())
    if ispractice:

        maxlapval = df["LAPS"].max()
        rounded_maxlapsval = int(math.ceil(maxlapval / 5.0)) * 5

        meansectorval = df[sectorno].mean()
        rounded_meansectorval = int(math.ceil(meansectorval / 5.0)) * 7.5

        minsectorval = df[sectorno].min()
        rounded_minsectorval = (int(math.floor(minsectorval / 5.0)) * 5) - 10

        maxectorval = df[sectorno].max()
        rounded_max123val = (int(math.floor(maxectorval / 5.0)) * 5) + 20

        excludeoutlap = st.radio("Exclude Out Laps?",("Yes","No"))
        if excludeoutlap == "Yes":
            newdf1 = df[df[sectorno] < meansectorval]
            topdf = newdf1.groupby(['NAME','N'])[sectorno].mean().reset_index(level='NAME').sort_values(by=sectorno,ascending=True)
            top10df = topdf.head(10)
            st.markdown("""#### Top 10 drivers based on average laptime in this session.""")
            st.text("Select the above dropdown to get performance by sector.")
            st.table(top10df[['NAME',sectorno]])
        else:
            newdf1 = df

        load_plot1(newdf1,0,rounded_maxlapsval,rounded_minsectorval,rounded_max123val,sectorno)

        newdf2 = df[df[sectorno] < rounded_meansectorval]
        load_plot4(newdf2,rounded_minsectorval,rounded_meansectorval,sectorno)

@st.cache
def load_data_session(session):
    sessionfile = ''
    jsondata = '{}'
    if session == 'r1P1':
        sessionfile = '1-Final_Practice1Data.json'
    elif session == 'r1P2':
        sessionfile = '1-Final_Practice2Data.json'
    elif session == 'r1P3':
        sessionfile = '1-Final_Practice3Data.json'
    elif session == 'r1M':
        sessionfile = '1-Final_MainRaceData.json'

    elif session == 'r2P1':
        sessionfile = '2-Final_Practice1Data.json'
    elif session == 'r2P2':
        sessionfile = '2-Final_Practice2Data.json'
    elif session == 'r2P3':
        sessionfile = '2-Final_Practice3Data.json'
    elif session == 'r2M':
        sessionfile = '2-Final_MainRaceData.json'

    elif session == 'r3P1':
        sessionfile = '3-Final_Practice1Data.json'
    elif session == 'r3P2':
        sessionfile = '3-Final_Practice2Data.json'
    elif session == 'r3P3':
        sessionfile = '3-Final_Practice3Data.json'
    elif session == 'r3M':
        sessionfile = '3-Final_MainRaceData.json'

    elif session == 'r4P1':
        sessionfile = '4-Final_Practice1Data.json'
    elif session == 'r4P2':
        sessionfile = '4-Final_Practice2Data.json'
    elif session == 'r4P3':
        sessionfile = '4-Final_Practice3Data.json'
    elif session == 'r4M':
        sessionfile = '4-Final_MainRaceData.json'

    elif session == 'r5P1':
        sessionfile = '5-Final_Practice1Data.json'
    elif session == 'r5P2':
        sessionfile = '5-Final_Practice2Data.json'
    elif session == 'r5P3':
        sessionfile = '5-Final_Practice3Data.json'
    elif session == 'r5M':
        sessionfile = '5-Final_MainRaceData.json'

    elif session == 'r6P1':
        sessionfile = '6-Final_Practice1Data.json'
    elif session == 'r6P2':
        sessionfile = '6-Final_Practice2Data.json'
    elif session == 'r6P3':
        sessionfile = '6-Final_Practice3Data.json'
    elif session == 'r6M':
        sessionfile = '6-Final_MainRaceData.json'

    elif session == 'r7P1':
        sessionfile = '7-Final_Practice1Data.json'
    elif session == 'r7P2':
        sessionfile = '7-Final_Practice2Data.json'
    elif session == 'r7P3':
        sessionfile = '7-Final_Practice3Data.json'
    elif session == 'r7M':
        sessionfile = '7-Final_MainRaceData.json'

    elif session == 'r8P1':
        sessionfile = '8-Final_Practice1Data.json'
    elif session == 'r8P2':
        sessionfile = '8-Final_Practice2Data.json'
    elif session == 'r8P3':
        sessionfile = '8-Final_Practice3Data.json'
    elif session == 'r8M':
        sessionfile = '8-Final_MainRaceData.json'

    elif session == 'r9P1':
        sessionfile = '9-Final_Practice1Data.json'
    elif session == 'r9P2':
        sessionfile = '9-Final_Practice2Data.json'
    elif session == 'r9P3':
        sessionfile = '9-Final_Practice3Data.json'
    elif session == 'r9M':
        sessionfile = '9-Final_MainRaceData.json'

    elif session == 'r10P1':
        sessionfile = '10-Final_Practice1Data.json'
    elif session == 'r10P2':
        sessionfile = '10-Final_Practice2Data.json'
    elif session == 'r10P3':
        sessionfile = '10-Final_Practice3Data.json'
    elif session == 'r10M':
        sessionfile = '10-Final_MainRaceData.json'

    elif session == 'r11P1':
        sessionfile = '11-Final_Practice1Data.json'
    elif session == 'r11P2':
        sessionfile = '11-Final_Practice2Data.json'
    elif session == 'r11P3':
        sessionfile = '11-Final_Practice3Data.json'
    elif session == 'r11M':
        sessionfile = '11-Final_MainRaceData.json'

    elif session == 'r12P1':
        sessionfile = '12-Final_Practice1Data.json'
    elif session == 'r12P2':
        sessionfile = '12-Final_Practice2Data.json'
    elif session == 'r12P3':
        sessionfile = '12-Final_Practice3Data.json'
    elif session == 'r12M':
        sessionfile = '12-Final_MainRaceData.json'

    elif session == 'r13P1':
        sessionfile = '13-Final_Practice1Data.json'
    elif session == 'r13P2':
        sessionfile = '13-Final_Practice2Data.json'
    elif session == 'r13P3':
        sessionfile = '13-Final_Practice3Data.json'
    elif session == 'r13M':
        sessionfile = '13-Final_MainRaceData.json'

    elif session == 'r14P1':
        sessionfile = '14-Final_Practice1Data.json'
    elif session == 'r14P2':
        sessionfile = '14-Final_Practice2Data.json'
    elif session == 'r14P3':
        sessionfile = '14-Final_Practice3Data.json'
    elif session == 'r14M':
        sessionfile = '14-Final_MainRaceData.json'

    elif session == 'r15P1':
        sessionfile = '15-Final_Practice1Data.json'
    elif session == 'r15P2':
        sessionfile = '15-Final_Practice2Data.json'
    elif session == 'r15P3':
        sessionfile = '15-Final_Practice3Data.json'
    elif session == 'r15M':
        sessionfile = '15-Final_MainRaceData.json'

    elif session == 'r16P1':
        sessionfile = '16-Final_Practice1Data.json'
    elif session == 'r16P2':
        sessionfile = '16-Final_Practice2Data.json'
    elif session == 'r16P3':
        sessionfile = '16-Final_Practice3Data.json'
    elif session == 'r16M':
        sessionfile = '16-Final_MainRaceData.json'

    elif session == 'r17P1':
        sessionfile = '17-Final_Practice1Data.json'
    elif session == 'r17P2':
        sessionfile = '17-Final_Practice2Data.json'
    elif session == 'r17P3':
        sessionfile = '17-Final_Practice3Data.json'
    elif session == 'r17M':
        sessionfile = '17-Final_MainRaceData.json'

    colorcodedf = get_colorcode()
    if sessionfile != '':     
        with open(sessionfile) as json_file:
            jsondata = json.load(json_file)
        data = pd.DataFrame(jsondata)
        if data.empty:
            data = data
        else:
            data = data.merge(colorcodedf,how="left",on="N")
    else:
        data = pd.DataFrame()
    return data

def load_plot1(df,startlap,endlap,mintime,maxtime,sectorno):
    df = df.sort_values(['N'],ascending=[1])
    color_dict = dict(zip(df.NAME, df.COLORCODE))
    fig = px.scatter(df, x="LAPS", y=sectorno, color="NAME",template="ggplot2",width=1200,height=600, hover_name="TYRE",trendline="lowess",color_discrete_map=color_dict)
    fig.update_xaxes(range=[startlap,endlap],title_text='Lap Number')
    fig.update_yaxes(range=[mintime,maxtime],title_text='Total sector time')
    fig.update_layout(plot_bgcolor ='#eeeeee',legend_bgcolor='#eeeeee')
    st.plotly_chart(fig)

def load_plot2(df,startlap,endlap,mintime,maxtime):
    df = df.sort_values(['N'],ascending=[1])
    color_dict = dict(zip(df.DRIVERCODE, df.COLORCODE))
    fig = px.scatter(df, x="LAPNO", y="S123", color="DRIVERCODE",template="ggplot2",width=1200,height=600, hover_name="TYRE",trendline="lowess",color_discrete_map=color_dict)
    fig.update_xaxes(range=[startlap,endlap],title_text='Lap Number')
    fig.update_yaxes(range=[mintime,maxtime],title_text='Total sector time')
    fig.update_layout(plot_bgcolor ='#eeeeee',legend_bgcolor='#eeeeee')
    st.plotly_chart(fig)

def load_plot3(df,mintime,maxtime):
    df = df.sort_values(['N'],ascending=[1])
    sns.set(style="darkgrid")
    st.text('Heatmap that indicate the total sector time for the main race. This gives an overview of push laps during the stint.')
    dataset = df.pivot("LAPNO", "DRIVERCODE", "S123")
    f, ax = plt.subplots(figsize=(25, 25))
    sns.heatmap(dataset, annot=True, cmap="cubehelix",vmin=mintime, vmax=maxtime, linewidths=0.5, fmt='g')
    sns.despine(left=True, bottom=True)
    st.pyplot()

def load_plot4(df,mintime,maxtime,sectorno):
    df = df.sort_values(['N'],ascending=[1])
    color_dict = dict(zip(df.NAME, df.COLORCODE))
    optiontyres = st.radio("By Tyre Compound or Overall?",("Overall","Tyre Compound"))
    if optiontyres == "Overall":
        fig = px.box(df, x="NAME", y=sectorno,color ="NAME",width=1200,height=600,color_discrete_map=color_dict)
        fig.update_xaxes(title_text='Name')
    if optiontyres =="Tyre Compound":
        fig = px.box(df, x="TYRECOMPOUND", y=sectorno,color="NAME",width=1200,height=600,color_discrete_map=color_dict)
        fig.update_xaxes(title_text='Tyre Compounds')
    fig.update_layout(plot_bgcolor ='#eeeeee',legend_bgcolor='#eeeeee')
    fig.update_yaxes(range=[mintime,maxtime],title_text='Total sector time')
   
    st.plotly_chart(fig)

def load_toptennracefinsh(df,sectorno):
    df = df[df['LAPNO'] != 1]
    meansectorval = df[sectorno].mean()
    newdf1 = df[df[sectorno] < meansectorval]
    topdf = newdf1.groupby(['NAME','N'])[sectorno].mean().reset_index(level='NAME').sort_values(by=sectorno,ascending=True)
    top10df = topdf.head(10)
    st.markdown("""#### Top 10 drivers based on average laptime.""")
    st.text("Select the above dropdown to get performance by sector.")
    st.table(top10df[['NAME',sectorno]])

def read_markdown(markdownfile):
    markdowncontentstr =""
    if markdownfile != "":
        markdowncontentstr = """{}""".format(open(markdownfile).read())
    return markdowncontentstr

def select_block_container_style():
    """Add selection section for setting setting the max-width and padding
    of the main block container"""

    max_width_100_percent = st.sidebar.checkbox("Set max width to 100%", False)
    if not max_width_100_percent:
        max_width = 1400
    else:
        max_width = 2000
    padding_top = 5
    padding_right = 1
    padding_left = 1
    padding_bottom = 10


    _set_block_container_style(
        max_width,
        max_width_100_percent,
        padding_top,
        padding_right,
        padding_left,
        padding_bottom,
    )

def get_colorcode():
    colorcodedf = pd.read_csv('ColorCodes.csv',index_col=False,header=0)
    return colorcodedf


def _set_block_container_style(max_width: int = 1200,max_width_100_percent: bool = False,padding_top: int = 5,padding_right: int = 1,padding_left: int = 1,padding_bottom: int = 10):
    if max_width_100_percent:
        max_width_str = f"max-width: 100%;"
    else:
        max_width_str = f"max-width: {max_width}px;"
    st.markdown(
        f"""
<style>
    .reportview-container .main .block-container{{
        {max_width_str}
        padding-top: {padding_top}rem;
        padding-right: {padding_right}rem;
        padding-left: {padding_left}rem;
        padding-bottom: {padding_bottom}rem;
    }}
    .reportview-container .main {{
        color: {COLOR};
        background-color: {BACKGROUND_COLOR};
    }}
</style>
""",
        unsafe_allow_html=True,
    )



if __name__ == '__main__':
    main()