import pandas as pd
import streamlit as st
import json

st.set_page_config(layout="wide")

# load data
f = open('data.json')
recipes_json = json.load(f)

# prepare data
recipes = pd.DataFrame(recipes_json)
ingredients = pd.DataFrame(recipes_json).explode('ingredients')
bar_dict = [
    {"ingredients":"rye", "have": False}, 
    {"ingredients":"cognac", "have": False}, 
    {"ingredients":"gin", "have": False}, 
    {"ingredients":"green chartreuse", "have": False}, 
    {"ingredients":"cointreau", "have": False}
]
bar_df = pd.DataFrame(bar_dict)
    
if 'filter_bar' not in st.session_state:
    st.session_state.filter_bar = recipes['name'].to_list()

if 'bar_df' not in st.session_state:
    st.session_state.bar_df = bar_df
    st.session_state.bar_df_edited = st.session_state.bar_df.copy() 

with st.expander("Search and filter recipes"):
    # name search
    name_search = st.text_input('Search recipes by name')
    if name_search == "":
        filter_name = recipes['name'].to_list()
    else:
        filter_1 = recipes['name'].str.contains(name_search)
        filter_name = recipes.loc[filter_1, 'name'].to_list()
    
    # ingredient filter
    options = st.multiselect(
        'Select ingredients to filter by:',ingredients['ingredients'].unique())
    filter_type = st.radio(
        "Specify type of ingredient filtering:",
        [
            'Recipe contains ANY of the specified ingredients', 
            'Recipe contains ALL of the specified ingredients'
        ]
    )
    if len(options)==0:
        filter_ingredient = recipes['name'].to_list()
    else:
        if filter_type == 'Recipe contains ANY of the specified ingredients':
            filter_1 = ingredients['ingredients'].isin(options)
            filter_ingredient = ingredients.loc[filter_1, 'name'].to_list()
        elif filter_type == 'Recipe contains ALL of the specified ingredients':
            filter_1 = ingredients['ingredients'].isin(options)
            ingredients['has_ingredient'] = 0
            ingredients.loc[filter_1, 'has_ingredient'] = 1
            pivot = ingredients.groupby('name').agg(sum_has_ingredients=('has_ingredient', 'sum')).reset_index()
            filter_ingredient = pivot.loc[pivot['sum_has_ingredients']==len(options), 'name'].to_list()
    
    # source filter
    source_options = st.multiselect(
        'Filter by source:',recipes['source'].unique())      
    if len(source_options)==0:
        filter_source = recipes['name'].to_list()
    else:
        filter_1 = recipes['source'].isin(source_options)
        filter_source = recipes.loc[filter_1, 'name'].to_list()
    
    # type filter
    type_options = st.multiselect(
        'Filter by type:',recipes['recipe_type'].unique())      
    if len(type_options)==0:
        filter_type = recipes['name'].to_list()
    else:
        filter_1 = recipes['recipe_type'].isin(type_options)
        filter_type = recipes.loc[filter_1, 'name'].to_list()

# def submit_bar():
    

# def reset_bar():
#     # st.session_state.bar_df = bar_df
#     st.session_state.bar_df_edited = bar_df
#     st.session_state.filter_bar = recipes['name'].to_list()
    

with st.expander("Home bar"):
    # input home bar
    st.session_state.bar_df_edited = st.data_editor(st.session_state.bar_df, disabled=["ingredients"], hide_index=True)
    
    ingredients_joined = ingredients.join(st.session_state.bar_df_edited.set_index('ingredients'), on='ingredients')
    ingredients_joined.replace({'have': {True: 1, False: 0}}, inplace=True)
    pivot = ingredients_joined.groupby('name').agg(sum_needs_ingredients=('have', 'count'), sum_has_ingredients=('have', 'sum')).reset_index()
    filter_1 = (pivot['sum_has_ingredients'] > 0) & (pivot['sum_needs_ingredients'] > 0)
    filter_2 = pivot['sum_needs_ingredients'] == pivot['sum_has_ingredients']
    filter_all = filter_1 & filter_2
    st.session_state.filter_bar = pivot.loc[filter_all, 'name'].to_list()
    
    # st.button(label='Submit Bar', on_click=submit_bar)
    # st.button(label='Reset', on_click=reset_bar)
        
st.header("Recipes")
filter_all = list(set(filter_name) & set(filter_ingredient) & set(filter_source) & set(filter_type) & set(st.session_state.filter_bar))
st.dataframe(recipes[recipes['name'].isin(filter_all)], hide_index=True)