from django.db import models
import django_tables2 as tables
from django_tables2.utils import A
from .models import Players
import django_filters

class TeamFilter(django_filters.FilterSet):
    class Meta:
        model = Players
        fields = ['paid']
            
class Player1Name(tables.Column):
    attrs = {
        "td": {
            'data-first-name': lambda record: record.p1_firstname,
            'data-last-name': lambda record: record.p1_surname,
            'style' : 'text-align: left;'
            }
    }
    def render(self, record):
        return "{}, {}".format(record.p1_surname, record.p1_firstname)
        
class Player2Name(tables.Column):
    attrs = {
        "td": {
            'data-first-name': lambda record: record.p2_firstname,
            'data-last-name': lambda record: record.p2_surname,
            'style' : 'text-align: left;'
            }
    }
    def render(self, record):
        return "{}, {}".format(record.p2_surname, record.p2_firstname)
        
class Handicap(tables.Column):
    def __init__(self, captain):
        super().__init__()
        self.isCaptain = captain
    def render(self, record):
        index = record.p2_index
        if self.isCaptain:
            index = record.p1_index
        return "{:.1f}".format(index)
    
class Course(tables.Column):        
    def __init__(self, captain):
        super().__init__()
        self.isCaptain = captain
        
    def render(self, record):
        #=IF(NOT(ISBLANK(O2)),ROUND(IF(N2="M",MIN(36,O2*101/113-68+64.4),MIN(36,O2*100/113-68+66.5)),0),"")
        index = record.p2_hcap
        if self.isCaptain:
            index = record.p1_hcap
        return "{:.0f}".format(index)

class TeamHandicap(tables.Column):
    def render(self, record):
        return "{:.1f}".format(record.hcap)

                
class TeamTable(tables.Table):
    p1_name = Player1Name()
    p2_name = Player2Name()
    id = tables.Column(linkify=True)
    
    class Meta:
        attrs = {"class": "table"}
        model = Players
        template_name = "django_tables2/semantic.html"
        fields = ("id", "paid", "p1_name", "p1_id",  "p1_club", "p2_name", "p2_id", "p2_club" )



class TeamHcapTable(tables.Table):
    p1_name = Player1Name()
    p1_hcap = Course(captain=True)
    p2_name = Player2Name()
    p1_index = Handicap(True)
    p2_index = Handicap(False)
    p2_hcap = Course(captain=False)
    hcap = TeamHandicap()
    id = tables.Column(linkify=True)
    
    class Meta:
        attrs = {"class": "table"}
        model = Players
        template_name = "django_tables2/semantic.html"
        fields = ("id", "p1_name", "p1_index", "p1_hcap", "p2_name", "p2_index", "p2_hcap", "hcap" )
        
        
class TeamScoresTable(tables.Table):
    p1_name = Player1Name()
    p2_name = Player2Name()
    hcap = TeamHandicap()
    id = tables.Column(linkify=True)
    
    class Meta:
        attrs = {"class": "table"}
        model = Players
        template_name = "django_tables2/semantic.html"
        fields = ("id", "p1_name", "p2_name", "hcap", "d1_gross", "d1_nett", "d2_gross", "d2_nett", "rank_gross", "rank_nett" )