import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import datetime
from datetime import date
import seaborn as sns
import calendar
import matplotlib.dates as mdates

st.markdown("## Detailed information for Incomes in Dairy Farm")   ## Main Title

################# Histogram Logic ########################

st.sidebar.markdown("### Choice type of income :")
option = st.sidebar.selectbox(
    'Choice type of income.',
    ('Income milk', 'Income animal',	'Income calves', 'Grand')) 



#st.set_page_config(layout="wide")
st.sidebar.title("Determinate period")
start_date1 = st.sidebar.date_input('start date', datetime.date(2019,1,1))
end_date1 = st.sidebar.date_input('end date', datetime.date(2022,1,1))

st.write('The selected income for you is :', option , "for period from", start_date1, "to", end_date1)

data = pd.read_excel(r'C:\Users\stani\Desktop\Webapp\st_zagora\income_data.xlsx') 
df = pd.DataFrame(data)
df['data'] = pd.to_datetime(df['data']).dt.date

df_res_period1=df[(df['data'] >= start_date1) & (df['data'] <= end_date1)]


if (option == 'Income milk'):
    income=df_res_period1['income_milk'] 
    total_income_milk = df_res_period1['income_milk'].sum()
    average_income_milk = df_res_period1['income_milk'].mean()
    st_dev_income_milk = df_res_period1['income_milk'].std()
    st.write('Total income from sells of milk for this period is ', round(total_income_milk,2))
    st.write('Average income from milk for month is ', round(average_income_milk,2))
    st.write('Standart deviation for income milk for month is ', round(st_dev_income_milk,2))
    
    fig, ax = plt.subplots(figsize=(15,8))
    x = np.arange(len(df_res_period1))
    ax.autoscale()
    print (x)
    year_month_formatter = mdates.DateFormatter("%b") # four digits for year, two for month
    half_year_locator = mdates.MonthLocator(interval=3)
    ax.xaxis.set_major_locator(half_year_locator)
    ax.xaxis.set_major_formatter(year_month_formatter) # formatter for major axis only

    width = 0.4
    plt.bar(df_res_period1['data'], df_res_period1['income_milk'],
       width, color='tab:green', label='income')
    plt.title('Income from milk in farm', fontsize=25)
    plt.xlabel('Months', fontsize=20)
    plt.xticks( fontsize=17)
    plt.axhline(average_income_milk, color='red', linestyle='--', linewidth=3, label='Average')
    plt.ylabel('Lev', fontsize=20)
    plt.yticks(fontsize=17)
    sns.despine(bottom=True)
    ax.grid(False)
    ax.tick_params(bottom=False, left=True)
    plt.legend(frameon=False, fontsize=15)
    #plt.show()
    st.pyplot(fig)


  
elif (option == 'Income animal'):
     
    income=df_res_period1['income_animal']
    total_income_animal = df_res_period1['income_animal'].sum()
    average_income_animal = df_res_period1['income_animal'].mean()
    st_dev_income_animal = df_res_period1['income_animal'].std()
    st.write('Total income animal =', round(total_income_animal,2))
    st.write('Average income milk =', round(average_income_animal,2))
    st.write('Standart deviation income milk =', round(st_dev_income_animal,2))
         
    fig, ax = plt.subplots(figsize=(10,5))
    x = np.arange(len(df_res_period1))
       
    year_month_formatter = mdates.DateFormatter("%b") # four digits for year, two for month
    half_year_locator = mdates.MonthLocator(interval=3)
    ax.xaxis.set_major_locator(half_year_locator)
    ax.xaxis.set_major_formatter(year_month_formatter) # formatter for major axis only
    
    width = 0.4
    ax.autoscale()
    plt.bar(x-0.2, df_res_period1['income_animal'],
    width, color='tab:green', label='income')
    plt.title('Income from sales of animals', fontsize=25)
    plt.xlabel('Months', fontsize=20)
    plt.xticks( fontsize=17)
    plt.axhline(average_income_animal, color='red', linestyle='--', linewidth=3, label='Average')
    plt.ylabel('Lev', fontsize=20)
    plt.yticks(fontsize=17)
    sns.despine(bottom=True)
    ax.grid(False)
    ax.tick_params(bottom=False, left=True)
    plt.legend(frameon=False, fontsize=15)
    #plt.show()
    st.pyplot(fig)

elif (option == 'Income calves'):
 
    income=df_res_period1['income_calves'] 
    total_income_calves = df_res_period1['income_calves'].sum()
    average_income_calves = df_res_period1['income_calves'].mean()
    st_dev_income_calves = df_res_period1['income_calves'].std()
    st.write('Total income calves =', round(total_income_calves,2))
    st.write('average income calve =', round(average_income_calves,2))
    st.write('standart deviation income calve =', round(st_dev_income_calves,2))
         
    fig, ax = plt.subplots(figsize=(10,5))
    x = np.arange(len(df_res_period1))
    print (x)
    width = 0.4
    ax.autoscale()
    plt.bar(x-0.2, df_res_period1['income_calves'],
    width, color='tab:green', label='income')
    plt.title('Income from sales of calves', fontsize=25)
    plt.xlabel('Months', fontsize=20)
    plt.xticks( fontsize=17)
    plt.axhline(average_income_calves, color='red', linestyle='--', linewidth=3, label='Average')
    plt.ylabel('Lev', fontsize=20)
    plt.yticks(fontsize=17)
    sns.despine(bottom=True)
    ax.grid(False)
    ax.tick_params(bottom=False, left=True)
    plt.legend(frameon=False, fontsize=15)
    #plt.show()
    st.pyplot(fig)

else:
 
    income=df_res_period1['grand']
    total_income_grand = df_res_period1['grand'].sum()
    average_income_grand = df_res_period1['grand'].mean()
    st_dev_income_grand = df_res_period1['grand'].std()
    st.write('Total income from grand =', round(total_income_grand,2))
    st.write('Average income from grand =', round(average_income_grand,2))
    st.write('Standart deviation of income from grand =', round(st_dev_income_grand,2))
         
    fig, ax = plt.subplots(figsize=(10,5))
    x = np.arange(len(df_res_period1))
    print (x)
    width = 0.4
    ax.autoscale()
    plt.bar(x-0.2, df_res_period1['grand'],
    width, color='tab:green', label='income')
    plt.title('Income from grand', fontsize=25)
    plt.xlabel('Months', fontsize=20)
    plt.xticks( fontsize=17)
    plt.axhline(average_income_grand, color='red', linestyle='--', linewidth=3, label='Average')
    plt.ylabel('Lev', fontsize=20)
    plt.yticks(fontsize=17)
    sns.despine(bottom=True)
    ax.grid(False)
    ax.tick_params(bottom=False, left=True)
    plt.legend(frameon=False, fontsize=15)
    #plt.show()
    st.pyplot(fig)
