############################################
# Imports for logic and basic table displays
############################################
import datetime as dt
import os
import time
from itertools import combinations
import gc

import pandas as pd
from flask import Flask
from flask import flash
from flask import render_template
from flask import request, session, redirect, url_for
from flask_login import LoginManager, logout_user
#########################
from flask_login import login_required, current_user
#########################
from flask_wtf import Form, FlaskForm
from wtforms import BooleanField, SubmitField, TextField, SelectField, StringField, IntegerField

from poker_classes.cards import Cards
from poker_classes.dealer import Dealer
from poker_classes.game import Game
from poker_classes.player import Player


#######Additional imports from clyde
# from flask_socketio import SocketIO, emit


class MasterControlForm(FlaskForm):
    reset_table = BooleanField(label="Reset Table?", default=False)
    rotate_dealer = BooleanField(label="Rotate Dealer?", default=False)
    take_ante = BooleanField(label="Take Ante?", default=False)
    new_deal = BooleanField(label="New deal?", default=False)
    first_bet = BooleanField(label="First Bet?", default=False)
    second_bet = BooleanField(label="Second Bet?", default=False)
    third_bet = BooleanField(label="Third Bet?", default=False)
    fourth_bet = BooleanField(label="Fourth Bet?", default=False)
    fifth_bet = BooleanField(label="Last Bet?", default=False)
    first_flip = BooleanField(label="First Flip?", default=False)
    second_flip = BooleanField(label="Second Flip?", default=False)
    third_flip = BooleanField(label="Third Flip?", default=False)

    declare_open = BooleanField(label="Declare Open", default=False)
    declare_closed = BooleanField(label="Declare Done", default=False)

    evaluate_now = BooleanField(label="Evaluate winner?", default=False)
    submit = SubmitField("Submit")

    seat_new_players = BooleanField(label="Seat new Players", default=False)
    remove_player = BooleanField(label="Remove Player", default=False)
    fold_player = BooleanField(label="Fold Player", default=False)

    winnings_l1 = IntegerField(label='Low_1',default=0)
    winnings_l2 = IntegerField(label='Low_2', default=0)
    winnings_l3 = IntegerField(label='Low_3', default=0)
    winnings_l4 = IntegerField(label='Low_4', default=0)

    winnings_h1 = IntegerField(label='High_1', default=0)
    winnings_h2 = IntegerField(label='High_2', default=0)
    winnings_h3 = IntegerField(label='High_3', default=0)
    winnings_h4 = IntegerField(label='High_4', default=0)

    distribute_winnings = BooleanField(label="Distribute Winnings", default=False)




class FullTableForm(FlaskForm):
    hand1_kf = SelectField(label="Hand 1", default='keep')
    hand2_kf = SelectField(label="Hand 2", default='keep')
    bet_action = SelectField(label="Action", default="None")


class DeclareForm(FlaskForm):
    hand1_hl = SelectField(label="Hand 1 H/L", default='undeclared')
    hand2_hl = SelectField(label="Hand 2 H/L", default='undeclared')
    submit = SubmitField("Take Action")


#########################

working_dir = os.getcwd()
app_dir = working_dir + '/poker_classes/'
player_dir = working_dir + '/existing_players/'
summary_dir =working_dir + '/performance_summaries/'

cards = Cards()
this_game = Game()
#this_game.set_pic_a_wheel()
this_game.set_three_phase()


dealer = Dealer()


login_manager = LoginManager()  # Sets up player views

############################################
# Player Initialization
############################################
alba = Player(player_dir, name='John Alba', nickname='Bronislav')
bornstein = Player(player_dir, name='Bill Murphy', nickname='Bornstein')
clyde = Player(player_dir, name='Bob Vincent', nickname='Clyde')
brian = Player(player_dir, name='Brian Mercer', nickname='Mercer')
ed = Player(player_dir, name='Ed Mulhern', nickname='Mr.Pink')
tardie = Player(player_dir, name='Michael Tardie', nickname='Tardie')
judogi = Player(player_dir, name='Bob Powers', nickname='Judogi')
jeff = Player(player_dir, name='Jeff Andersen', nickname='Jeff')
degroot = Player(player_dir, name='Henry DeGroot', nickname='Grout')
bart = Player(player_dir, name='Steve Bart', nickname='Bartman')
smith = Player(player_dir, name='Walt Smith', nickname='Smitty')

possible_players = [alba, bornstein, brian, clyde, degroot, ed, jeff,
                    judogi, tardie, bart, smith]

# starting_funds = dealer.initial_player_funds
for i,p in enumerate(possible_players):
    p.bankroll += dealer.initial_player_funds
    p.starting_funds = dealer.initial_player_funds
    possible_players[i]=p

### Old testing, no longer needed####
#players = [alba, bornstein, clyde, brian, ed]
#players = [alba, bornstein, clyde]
#players = [bornstein]
#players = [alba, bornstein]
#players = [clyde, tardie, judogi, brian, ed, bornstein, jeff]
#players = [jeff, brian, bornstein]

players = []

player_dict = {}
# Removing Funds until login corrected
# for i, p in enumerate(players):
#    p.add_funds(500)  # add funds
#    p.player_position = i  # set table position




#########################
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 10

# App config from Clyde
app.config["SECRET_KEY"] = "yousecntuch"
app.debug = True

login_manager = login_manager.init_app(app)



@app.before_first_request
def initialize():
    print("Called only once, when the first request comes in")
    #moved inside ap
    dealer.set_game_number(summary_dir)
    print(f"Set Game number to {dealer.game_number}")

@app.route('/')
def index():
    session.clear()
    return "<h1>Home page for tonight's picawheel game</h1>"


@app.route('/login', methods=['GET', 'POST'])
def login():
    global players, this_game

    if not session.get('offered_login'):
        session['offered_login'] = True
        return render_template('login.html')

    if session.get('logged_in'):
        return f"{session['username']} is already logged in."

    elif request.method in ['POST', 'GET']:

        #session['username'] = request.form.get('username')
        tmp_name = request.form.get('username')
        valid_ids = [p.p_nickname for p in possible_players]

        if tmp_name in valid_ids:
            if (tmp_name not in [p.p_nickname for p in this_game.players_logged_in]) and\
                (tmp_name not in [p.p_nickname for p in players]):
                session['username']=tmp_name

                for guy in possible_players:
                    if guy.p_nickname == session['username']:
                        guy.reset_player_from_master_control() # Fix for jinja code
                        dealer.players_waiting_to_enter.append(guy)

                        dealer.waiting_names=[x.p_nickname for x in dealer.players_waiting_to_enter]

                        this_game.players_logged_in.append(guy)
                        print(f"Players logged in: {[p.p_nickname for p in this_game.players_logged_in]}")
                        # return f"Hello {guy.p_nickname}.  Thank-you for logging in.  Please wait patiently."
                        return redirect(url_for("full_table"))

                return f"{guy.p_nickname}:  You pooched your login.  Please return to kindergarten."

            else:
                tmp = f"{tmp_name} is already logged in.  You can't log in as someone else."
                #session.clear()
                return f"{tmp}"

        else:
            return "Invalid Id.  I don't know you, go away."

    else:
        session.clear()
        # return render_template('login2.html')
        return redirect(url_for('login.html'))


@app.route('/full_table', methods=['GET', 'POST'])
def full_table():
    global players, this_game

    form = FullTableForm()

    # Check for folded players
    dealer.check_which_players_are_folded(players)

#################################################################
    # Check if player has been removed:
    if session['username'] in dealer.dead_guys:
        dead_guy = dealer.dead_guys.pop(dealer.dead_guys.index(session['username']))
        print(f"Deleteing cookie from {dead_guy}")
        print(f"Remaining deadguys: {dealer.dead_guys}")
        logout_user()
        return f"{dead_guy} has been removed from the game."
#################################################################
    if dealer.showdown:
        #setup dealer.rolling_df
        new_players = []
        for i, p in enumerate(players):

            p.hands_pr[0] = dealer.convert_value_hand_to_display(p.hands[0])
            p.hands_pr[1] = dealer.convert_value_hand_to_display(p.hands[1])
            if len(p.common_cards) == 1 and isinstance(p.common_cards, list):
                p.common_cards_pr = dealer.convert_value_card_to_display(p.common_cards[0])
            else:
                p.common_cards_pr = dealer.convert_value_hand_to_display(p.common_cards)

            new_players.append(p)
            players[i] = p

        return render_template('table_showdown.html', dealer=dealer,
                               players=new_players,high_hand_df=dealer.high_hand_df_dis,
                               low_hand_df=dealer.low_hand_df_dis,rolling_df=dealer.rolling_df)

    # Betting Complete housekeeping, make round summary
    if dealer.betting_complete:
        print(f"{dealer.active_player},{dealer.betting_complete}")
        dealer.num_raises = 0
        if not dealer.made_round_summary:
            for i,p in enumerate(players):
                p.get_number_hands()
                p.hands_by_round.append(p.num_hands)
                p.in_pot_by_round.append(p.in_pot_this_round)
                p.in_pot_this_round = 0

                ## Calculate post round bankroll
                p.total_winnings = sum(p.evening_winnings)
                print(f"{p.p_nickname} evening_winnings:{p.evening_winnings} tot:{p.total_winnings}")
                p.total_bets = sum(p.evening_bets)
                print(f"{p.p_nickname} evening_bets:{p.evening_bets} tot:{p.total_bets}")
                p.bankroll2 = p.starting_funds + p.total_winnings - p.total_bets
                print(p.p_nickname,"roll: ",p.bankroll2,"win: ",p.total_winnings,"bets: ",p.total_bets)

                players[i] = p

            dealer.made_round_summary = True
            dealer.make_hand_plot_no_pyplot(players)
            return redirect(url_for("full_table"))
    else:
        dealer.made_round_summary = False

    # Betting complete before declare
    if dealer.betting_complete and not dealer.declare_open and not dealer.declare_done:

        if session['username'] in dealer.waiting_names: # Waiting Bandaid
            this_player = dealer.make_player_cards_no_options(dealer.players_waiting_to_enter, session['username'], cards)
        else:
            this_player = dealer.make_player_cards_no_options(players, session['username'], cards)

        this_player = dealer.make_your_hand_display_cards(this_player)
        dealer.check_which_players_are_folded(players)
        dealer.flips_complete=len([x for x in dealer.common_cards_flipped if x == True])

        dealer.calculate_bankrolls(players)
        return render_template('round_summary.html', dealer=dealer,
                               players=players, this_player=this_player, form=form)

    # Betting complete after declare
    if dealer.betting_complete and dealer.declare_done:

        # Set active player to "No One" forcing auto refresh
        dealer.active_player = "No One"

        if session['username'] in dealer.waiting_names:  # Waiting Bandaid
            this_player = dealer.make_player_cards_no_options(dealer.players_waiting_to_enter, session['username'],
                                                              cards)
        else:
            this_player = dealer.make_player_cards_no_options(players, session['username'], cards)

        this_player = dealer.make_your_hand_display_cards(this_player)
        dealer.check_which_players_are_folded(players)
        dealer.flips_complete = len([x for x in dealer.common_cards_flipped if x == True])

        print("post declare: dealer.player_funds_df")
        dealer.calculate_bankrolls(players)
        return render_template('round_summary.html', dealer=dealer,
                               players=players, this_player=this_player, form=form)

    #Jinja bandaid
    if session['username'] in dealer.waiting_names:
        for p in dealer.players_waiting_to_enter:
            if p.p_nickname == session['username']:
                this_guy = p
    else:
        for p in players:
            if p.p_nickname == session['username']:
                this_guy = p


    if dealer.declare_open:
        print(f"Declare open, {this_guy.p_nickname} trying to play.")
        print(f"His declare flag is: {this_guy.declare_complete}")
        if session['username'] in dealer.waiting_names:  # Waiting Bandaid
            this_player = dealer.make_player_cards_no_options(dealer.players_waiting_to_enter, session['username'],
                                                              cards)
        else:
            this_player = dealer.make_player_cards_no_options(players, session['username'], cards)

        this_player = dealer.make_your_hand_display_cards(this_player)

        # this_guy declare is not done
        if not this_guy.declare_complete:
            return redirect(url_for("declare"))

        # this_guy declare is done
        if this_guy.declare_complete:

            dealer.players_not_declared=[]
            for p in players:
                if (not p.declare_complete) and (p.in_hand):
                    dealer.players_not_declared.append(p.p_nickname)

            return render_template('player_base_table_declare_wait_state.html', dealer=dealer,
                                   players=players, this_player=this_player, form=form)

    # Active player form processing
    if session['username'] == dealer.active_player:
        print(f"Active Player: {session['username']}")
        print(f"dealer.declare_done: {dealer.declare_done}")

        # Process reset
        if (dealer.perform_reset):
            for p in players:
                new_players.append(p.reset_player_from_master_control())
            players = new_players
            dealer.reset_table(players, this_game)
            gc.collect()

            hand1_kf = 'keep'
            hand2_kf = 'keep'
            bet_action = 'Check'
            form.hand1_kf = 'keep'
            form.hand2_kf = 'keep'
            form.bet_action = 'Check'
            dealer.perform_reset = False
            return redirect(url_for("full_table"))

        # Process active player form
        if form.validate_on_submit:

            hand1_kf = request.form.get('hand1_kf')
            hand2_kf = request.form.get('hand2_kf')
            bet_action = request.form.get('bet_actions')

            if hand1_kf == 'fold':
                for i, p in enumerate(players):
                    if session['username'] == p.p_nickname:
                        p.hands[0] = 'folded'
                        p.hands_pr[0] = 'folded'
                        players[i] = p

            if hand2_kf == 'fold':
                for i, p in enumerate(players):
                    if session['username'] == p.p_nickname:
                        p.hands[1] = 'folded'
                        p.hands_pr[1] = 'folded'

                        # Fold common cards
                        if (p.hands[0] == 'folded') & (p.hands[1] == 'folded'):
                            p.common_cards = ['folded']
                            p.common_cards_pr = ['folded']

                        players[i] = p

            # Set basic display parameters and this_player variable for processing
            this_player = dealer.make_player_cards_no_options(players, session['username'], cards)
            this_player = dealer.make_your_hand_display_cards(this_player)

            # Check to see if player folded.
            this_player.get_number_hands()
            if this_player.num_hands == 0:
                this_player.common_cards = ['folded']
                this_player.common_cards_pr = ['folded']

            #  Fix for folding issue
            for i, p in enumerate(players):
                if this_player.p_nickname == p.p_nickname:
                    players[i] = this_player

            # Process form actions
            action = 'check'
            action_amount = 0
            if this_player.num_hands == 0:
                action = 'fold'

            elif bet_action == 'check':
                action = 'check'

            elif bet_action == 'call':
                action = 'call'

            elif bet_action == 'bet_5':
                action = 'bet'
                action_amount = 5

            elif bet_action == 'bet_10':
                action = 'bet'
                action_amount = 10

            elif bet_action == 'bet_15':
                action = 'bet'
                action_amount = 15

            elif bet_action == 'bet_20':
                action = 'bet'
                action_amount = 20

            else:
                action = 'None'
                action_amount = 0

            #######################################
            # Set table display for number of hands
            tmp_2 = 0
            tmp_1 = 0
            tmp_folded = 0

            for p in players:
                if session['username'] == p.p_nickname:
                    p = dealer.analyze_action(action, action_amount, players, p)
                fold_count=0
                for h in p.hands:
                    if h == 'folded':
                        fold_count+=1
                if fold_count == 0:
                    tmp_2 +=1
                elif fold_count == 1:
                    tmp_1 += 1
                else:
                    tmp_folded += 1

            dealer.players_w_two_hands = tmp_2
            dealer.players_w_one_hand = tmp_1
            dealer.players_folded = tmp_folded
            #######################################

            tmp = dealer.new_betting_order[0]
            dealer.active_player = tmp.p_nickname
            dealer.check_which_players_are_folded(players)

            print(f"this player: {this_player.p_nickname} ACTIVE")

            if this_player and dealer.active_player == session['username']:
                return render_template('player_base_table_active.html', dealer=dealer,
                                       players=players, this_player=this_player, form=form)

            elif this_player and dealer.active_player != session['username']:
                return render_template('player_base_table.html', dealer=dealer,
                                       players=players, this_player=this_player, form=form)
            else:
                return f"Houston, we have a problem.  {session['username']} is attempting to access table, and I am confused about what to do."


    else:
        try:
            this_player = dealer.make_player_cards_no_options(players, session['username'], cards)
            this_player = dealer.make_your_hand_display_cards(this_player)

            print(f"this player: {this_player.p_nickname} is not currently active")
        except:
            pass

        if this_player:
            return render_template('player_base_table.html', dealer=dealer,
                                   players=players, this_player=this_player)
        elif players == []:
            print("Empty players")
            return render_template('player_base_table.html', dealer=dealer,
                                   players=players, this_player=this_player)

        elif (session['username'] in dealer.waiting_names):
            for p in this_game.players_logged_in:
                if p.p_nickname == session['username']:
                    this_player=p
                    this_player.expect_long_wait=True

            return render_template('player_base_table.html', dealer=dealer,
                                   players=players, this_player=this_player)

        else:
            sorry_message = f"Sorry.  {session['username']} is not currently welcome at this table, " +\
                          f"likely because you pooched your login, \n"+\
                           f" negelcted to clear your cookies before starting, (or you are trying to cheat.)  \n\n" +\
                          f"Please wait while  we investigate."
            return sorry_message




@app.route('/declare', methods=['GET', 'POST'])
def declare():
    global players, this_game, cards

    form = DeclareForm()

    # set this_guy to player requesting declare info

    if (session['username'] in dealer.waiting_names):
        for p in dealer.players_waiting_to_enter:
            if p.p_nickname == session['username']:
                this_guy = p
                this_guy.expect_long_wait = True
                return render_template('player_base_table.html', dealer=dealer,
                                       players=players, this_player=this_guy)

    else:
        for p in players:
            if p.p_nickname == session['username']:
                this_guy = p

    if form.validate_on_submit:

        if this_guy.declare_complete:
            return redirect(redirect(url_for("full_table")))

        if not this_guy.declare_start:
            this_guy.declare_start = True

            for i, p in enumerate(players):
                if p.p_nickname == session['username']:
                    players[i] = this_guy

            if session['username'] in dealer.waiting_names:  # Waiting Bandaid
                this_player = dealer.make_player_cards_no_options(dealer.players_waiting_to_enter, session['username'],
                                                                  cards)
            else:
                this_player = dealer.make_player_cards_no_options(players, session['username'], cards)

            this_player = dealer.make_your_hand_display_cards(this_player)
            return render_template('player_base_table_declare.html', dealer=dealer,
                                   players=players, this_player=this_player)

        if this_guy.hands[0] != 'folded':
            hand1_hl = request.form.get('hand1_hl')
            if hand1_hl == 'high':
                this_guy.hands_hi_lo[0] = 'high'
            else:
                this_guy.hands_hi_lo[0] = 'low'

        if this_guy.hands[1] != 'folded':
            hand2_hl = request.form.get('hand2_hl')
            if hand2_hl == 'high':
                this_guy.hands_hi_lo[1] = 'high'
            else:
                this_guy.hands_hi_lo[1] = 'low'

        this_guy.declare_complete = True

        for i, p in enumerate(players):
            if p.p_nickname == session['username']:
                players[i] = this_guy

        this_player = dealer.make_player_cards_no_options(players, session['username'], cards)
        this_player = dealer.make_your_hand_display_cards(this_player)

        for i, p in enumerate(players):
            if p.p_nickname == session['username']:
                players[i] = this_player
        return redirect(url_for("full_table"))


@app.route('/master_control', methods=['GET', 'POST'])
def master_control():
    global players
    form = MasterControlForm()

    if form.validate_on_submit:
        reset_table = form.reset_table.data
        rotate_dealer = form.rotate_dealer.data
        take_ante = form.take_ante.data
        new_deal = form.new_deal.data
        first_bet = form.first_bet.data
        second_bet = form.second_bet.data
        third_bet = form.third_bet.data
        fourth_bet = form.fourth_bet.data
        fifth_bet = form.fifth_bet.data
        flip1 = form.first_flip.data
        flip2 = form.second_flip.data
        flip3 = form.third_flip.data

        # New Player
        seat_new_players = form.seat_new_players.data
        if seat_new_players:

            # Reset table and existing players
            new_players = []
            session['declare_complete'] = False
            session['declare_start'] = False
            dealer.reset_table(players, this_game)
            dealer.perform_reset = True

            for i, p in enumerate(players):
                players[i] = p.reset_player_from_master_control()

            # Seat players legacy
            print(f"{len(players)}Players {[x.p_nickname for x in players]}")
            for p in dealer.players_waiting_to_enter:
                p.reset_player_from_master_control() #Trial to fix entering problem.
                print(f"Working on {p.p_nickname}")
                dealer.insert_new_player(players, p)
                print(f"{len(players)}Players {[x.p_nickname for x in players]}")
                for i,pl in enumerate(players):
                    print(f"Updating player (pl): {pl.p_nickname}")
                    if pl.p_nickname == p.p_nickname:
                        print(f"Found p.p_nickname {p.p_nickname}")
                        p.expect_long_wait=False
                        players[i]=p

            dealer.players_waiting_to_enter = []
            dealer.waiting_names = []
            form.seat_new_players.data = False
            return render_template('master_control.html', form=form, name='Bornstein',
                                   players=players, this_game=this_game, dealer=dealer)
        # Remove Player
        remove_player_flag = form.remove_player.data
        if remove_player_flag:
            guy = request.form.get("player_to_remove")
            print(f"Found player_to_remove_flag:  {guy}")
            for p in players:
                if guy == p.p_nickname:
                    dead_guy = players.pop(players.index(p))
                    dealer.dead_guys.append(dead_guy.p_nickname)

                    for pl in this_game.players_logged_in:
                        if pl.p_nickname == guy:
                            this_game.players_logged_in.pop(this_game.players_logged_in.index(pl))
                    print(f"Removed {guy} from players and players_logged_in")
                    print(f"List of dead guys: {dealer.dead_guys}")
                    print(f"Remaining Players: {[p.p_nickname for p in players]}")
                    return redirect(url_for("master_control"))

        # Fold Player
        fold_player_flag = form.fold_player.data
        if fold_player_flag:
            guy = request.form.get("player_to_fold")
            print(f"Found player_to_fold_flag:  {guy}")
            for p in players:
                if guy == p.p_nickname:
                    print(f"self.new_betting_order: {dealer.new_betting_order}")
                    dealer.force_fold_player(guy,players)

                    print(f"Folded {guy} from hand")
                    print(f"Now the active player is: {dealer.active_player}")
                    print(f"Betting Order: {[p.p_nickname for p in dealer.new_betting_order]}")

            form.fold_player.data = False
            name=session['username']
            return render_template('master_control.html', form=form, name=name,
                                           players=players, this_game=this_game, dealer=dealer)

        distribute_winnings_flag = form.distribute_winnings.data
        if distribute_winnings_flag:
            guy_l1 = request.form.get("winner_l1")
            win_l1 = form.winnings_l1.data

            for i,p in enumerate(players):
                if guy_l1 == p.p_nickname:
                    p.bankroll+=win_l1
                    players[i]=p

            guy_l2 = request.form.get("winner_l2")
            win_l2 = form.winnings_l2.data

            for i, p in enumerate(players):
                if guy_l2 == p.p_nickname:
                    p.bankroll += win_l2
                    players[i] = p

            guy_l3 = request.form.get("winner_l3")
            win_l3 = form.winnings_l3.data

            for i, p in enumerate(players):
                if guy_l3 == p.p_nickname:
                    p.bankroll += win_l3
                    players[i] = p

            guy_l4 = request.form.get("winner_l4")
            win_l4 = form.winnings_l4.data

            for i, p in enumerate(players):
                if guy_l4 == p.p_nickname:
                    p.bankroll += win_l4
                    players[i] = p
            # Highs
            guy_h1 = request.form.get("winner_h1")
            win_h1 = form.winnings_h1.data
            for i,p in enumerate(players):
                if guy_h1 == p.p_nickname:
                    p.bankroll+=win_h1
                    players[i]=p

            guy_h2 = request.form.get("winner_h2")
            win_h2 = form.winnings_h2.data

            for i, p in enumerate(players):
                if guy_h2 == p.p_nickname:
                    p.bankroll += win_h2
                    players[i] = p

            guy_h3 = request.form.get("winner_h3")
            win_h3 = form.winnings_h3.data

            for i, p in enumerate(players):
                if guy_h3 == p.p_nickname:
                    p.bankroll += win_h3
                    players[i] = p

            guy_h4 = request.form.get("winner_h4")
            win_h4 = form.winnings_h4.data

            for i, p in enumerate(players):
                if guy_h4 == p.p_nickname:
                    p.bankroll += win_h4
                    players[i] = p
            dealer.show_winnings = True
            for p in players:
                print(f"{p.p_nickname} {p.bankroll}")

            return render_template('master_control.html', form=form, name='Bornstein',
                                   players=players, this_game=this_game, dealer=dealer)

        declare_open = form.declare_open.data
        if declare_open:
            dealer.declare_open = True
            dealer.check_which_players_are_folded(players)
            for fp in dealer.folded_players_list:
                for i,p in enumerate(players):
                    if p.p_nickname == fp:
                        p.declare_complete = True
                        players[i] = p

        declare_closed = form.declare_closed.data
        if declare_closed:
            dealer.declare_open = False
            dealer.declare_done = True
            dealer.betting_complete = False
            dealer.betting_round_number += 1
            dealer.new_betting_order = []
            dealer.betting_complete = False
            dealer.new_bet = False
            dealer.check_count = 0
            dealer.num_raises = 0
            dealer.who_opened = 'No one'
            dealer.last_raise = 'No one'
            dealer.active_player = 'No One'

            if dealer.calc_highs_lows:
                for i,p in enumerate(players):
                    for h in p.hands_hi_lo:
                        if h == 'high':
                            dealer.high_hands +=1
                        if h == 'low':
                            dealer.low_hands += 1
                dealer.calc_highs_lows = False

            p: Player
            for i, p in enumerate(players):
                p.this_round_per_side = 0
                players[i] = p

        #################################################
        evaluate_now = form.evaluate_now.data
        if evaluate_now and not dealer.done_scoring:
            dealer.evaluate_hands_calc_winnings(players, this_game)
            dealer.save_summary_data(summary_dir)

        submit_value = form.submit.data

        #############################################
        flip_controls = [flip1, flip2, flip3]
        for i, f in enumerate(dealer.common_cards_flipped):
            if dealer.common_cards_flipped[i]:
                pass
            else:
                if flip_controls[i] == dealer.common_cards_flipped[i]:
                    pass
                else:
                    dealer.common_cards_flipped[i] = True
                    dealer.betting_complete = False
                    dealer.betting_round_number = i
                    dealer.new_betting_order = []
                    dealer.betting_complete = False
                    dealer.new_bet = False
                    dealer.check_count = 0
                    dealer.num_raises = 0
                    dealer.who_opened = 'No one'
                    dealer.last_raise = 'No one'
                    dealer.active_player = 'No One'

                    p: Player
                    for i, p in enumerate(players):
                        p.this_round_per_side = 0
                        players[i] = p

        ##############
        # Calculate Round Action:
        rounds = [first_bet, second_bet, third_bet, fourth_bet, fifth_bet]

        for i, r in enumerate(rounds):
            if dealer.betting_rounds[i]:
                pass
            else:
                dealer.betting_rounds[i] = True
                dealer.betting_complete = False

    if (session['username'] == 'Bornstein') or (session['username'] == 'Clyde'):
        name = session['username']

        if reset_table:
            print('resetting')
            new_players = []
            session['declare_complete'] = False
            session['declare_start'] = False
            dealer.reset_table(players, this_game)
            dealer.perform_reset = True

            print("Flagging Reset")
            for i, p in enumerate(players):
                players[i] = p.reset_player_from_master_control()
                print((p.p_nickname, p.hands, p.common_cards))

            for p in players:
                if p.p_nickname == 'Bornstein':
                    print(p.p_nickname, p.hands, p.common_cards)
            return redirect(url_for('master_control'))

        if rotate_dealer:
            new_order=dealer.rotate_deal(players)
            for i,p in enumerate(new_order):
                players[i]=p
                return redirect(url_for('master_control'))

        if take_ante and not dealer.ante_taken:
            for i,p in enumerate(players):
                p.bankroll -= 10 #automatic ante
                p.in_pot += 10 #automatic ante
                p.evening_bets.append(10)
                players[i] = p
                dealer.pot += 10
            dealer.ante_taken = True
            form.take_ante.data = True

        if new_deal and not dealer.deal_complete:
            dealer.reset_table(players, this_game)
            dealer.hand_in_progress = True
            shuffled = dealer.deal_cards(players, this_game)

            dealer.deal_complete = True
            dealer.first_deal = False
            form.new_deal.data = True
            dealer.players_w_two_hands = len(players)
            dealer.calc_hi_low_pots()

            return render_template('master_control.html', form=form, name=name,
                                   players=players, this_game=this_game, dealer=dealer)

        if first_bet:

            new_players = dealer.get_betting_order(players)
            for i, p in enumerate(new_players):
                players[i] = new_players[i]
            print(' ')
            print(f"At first_bet: new_betting_order: {dealer.new_betting_order}")
            tmp = dealer.new_betting_order[0]
            dealer.active_player = tmp.p_nickname
            print(f"Active Player: {dealer.active_player}")

        return render_template('master_control.html', form=form, name=name,
                               players=players, this_game=this_game, dealer=dealer)

    else:
        return f"You are not Bornstein or Clyde, Fuck Off!"

if __name__ == '__main__':
    #app.run(host='0.0.0.0', debug=True)
    #app.run(host='0.0.0.0')
    app.run(debug=True)
