# Create your views here.
import logging

from django.shortcuts import render

LOGGER = logging.getLogger(__name__)

from django.views.generic import TemplateView
from django.views.generic.edit import FormView
from django.views.generic.base import View
from django.http import HttpResponse, HttpResponseRedirect

from .forms import TournamentInfoForm
from .forms import FORM_FIELD_TO_PLAYER_OBJECT
from tournament import Tournament


class HelloView(View):

    def get(self, request):
        return HttpResponse("Hello world")


class GameSimulationView(FormView):
    template_name = 'simulation.html'
    form_class = TournamentInfoForm
    success_url = '/'

    def _append_label_for_legend(self, payoff_list):
        for player_type in FORM_FIELD_TO_PLAYER_OBJECT.values():
            for payoff in payoff_list:
                if payoff['color'] == player_type.color:
                    payoff['label'] = player_type.name_prefix
                    break

    def form_valid(self, form):
        form_data = form.cleaned_data
        LOGGER.info('{}'.format(form_data))
        players = []
        for key, num in form_data.items():
            players.extend([FORM_FIELD_TO_PLAYER_OBJECT[key](i) for i in range(num)])
        LOGGER.info('{}'.format(players))
        tournament = Tournament(players)
        tournament.start_tournament()
        simulation_data = tournament.get_simulation_data()
        self._append_label_for_legend(simulation_data['payoff_list'])
        LOGGER.info('{}'.format(simulation_data))
        context = {'simulation_data': simulation_data, 'form': form}
        return render(self.request, 'simulation.html', context)

@@ -1,30 +1,4 @@
-# Create your views here.%0A
 impo
@@ -49,241 +49,54 @@
 der%0A
-%0ALOGGER = logging.getLogger(__name__)%0A%0Afrom django.views.generic import TemplateView%0Afrom django.views.generic.edit import FormView%0Afrom django.views.generic.base import View%0Afrom django.http import HttpResponse, HttpResponseRedirect
+from django.views.generic.edit import FormView
 %0A%0Afr
@@ -217,102 +217,43 @@
 nt%0A%0A
-%0Aclass HelloView(View):%0A%0A    def get(self, request):%0A        return HttpResponse(%22Hello world%22
+LOGGER = logging.getLogger(__name__
 )%0A%0A%0A
@@ -700,113 +700,49 @@
 def 
-form_valid(self, form):%0A        form_data = form.cleaned_data%0A        LOGGER.info('%7B%7D'.format(form_data))
+_generate_players(self, tournament_info):
 %0A   
@@ -787,17 +787,23 @@
  in 
-form_data
+tournament_info
 .ite
@@ -906,40 +906,62 @@
     
-LOGGER.info('%7B%7D'.format(
+return players%0A%0A    def _run_simulation(self, 
 players)
 )%0A  
@@ -956,17 +956,17 @@
 players)
-)
+:
 %0A       
@@ -1178,48 +1178,178 @@
     
-LOGGER.info('%7B%7D'.format(simulation_data)
+return simulation_data%0A%0A    def form_valid(self, form):%0A        players = self._generate_players(form.cleaned_data)%0A        simulation_data = self._run_simulation(players
 )%0A  

clean code and refactor