#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sqlalchemy import and_, or_

from .common import Base, session_scope


class Player(Base):
    __tablename__ = 'players'
    __autoload__ = True

    ALTERNATE_VALUE_KEYWORDS = [
        'alternate_last_names',
        'alternate_first_names',
        'alternate_positions',
    ]

    def __init__(self, nhl_id, last_name, first_name, position, **kwargs):
        self.player_id = int(nhl_id)
        self.last_name = last_name
        self.first_name = first_name
        self.position = position

        for keyword in self.ALTERNATE_VALUE_KEYWORDS:
            if keyword in kwargs:
                self.set_keyword_argument(keyword, kwargs[keyword])

    def set_keyword_argument(self, keyword, value):
        if value is not None and type(value) is not list:
            value = [value]
        setattr(self, keyword, value)

    @property
    def name(self):
        return " ".join((self.first_name, self.last_name))

    @classmethod
    def find_by_id(self, nhl_id):
        with session_scope() as session:
            try:
                player = session.query(Player).filter(
                    Player.player_id == nhl_id
                ).one()
            except:
                player = None
            return player

    @classmethod
    def find_by_name(self, first_name, last_name):
        with session_scope() as session:
            try:
                player = session.query(Player).filter(
                    and_(
                        Player.first_name == first_name,
                        Player.last_name == last_name
                    )
                ).one()
            except:
                player = None
            return player

    @classmethod
    def find_by_name_position(self, first_name, last_name, position):
        with session_scope() as session:
            try:
                player = session.query(Player).filter(
                    and_(
                        Player.first_name == first_name,
                        Player.last_name == last_name,
                        Player.position == position
                    )
                ).one()
            except:
                player = None
            return player

    @classmethod
    def find_by_name_extended(self, first_name, last_name):
        with session_scope() as session:
            try:
                player = session.query(Player).filter(
                    and_(
                        or_(
                            Player.first_name == first_name,
                            Player.alternate_first_names.any(first_name)
                        ),
                        or_(
                            Player.last_name == last_name,
                            Player.alternate_last_names.any(last_name)
                        )
                    )
                ).one()
            except:
                player = None
            return player

    def __str__(self):
        return "[%d] %s" % (self.player_id, self.name)

    def __eq__(self, other):
        return self.player_id == other.player_id

    def __ne__(self, other):
        return not self == other

    def __gt__(self, other):
        return self.last_name > other.last_name

    def __lt__(self, other):
        return self.last_name < other.last_name

@@ -72,16 +72,22 @@
 nd_, or_
+, func
 %0A%0Afrom .
@@ -1974,32 +1974,43 @@
                 
+func.lower(
 Player.first_nam
@@ -2002,32 +2002,33 @@
 layer.first_name
+)
  == first_name,%0A
@@ -2017,32 +2017,40 @@
 e) == first_name
+.lower()
 ,%0A              
@@ -2051,32 +2051,43 @@
                 
+func.lower(
 Player.last_name
@@ -2078,32 +2078,33 @@
 Player.last_name
+)
  == last_name,%0A 
@@ -2092,32 +2092,40 @@
 me) == last_name
+.lower()
 ,%0A              
@@ -2126,32 +2126,43 @@
                 
+func.lower(
 Player.position 
@@ -2160,16 +2160,17 @@
 position
+)
  == posi
@@ -2173,16 +2173,24 @@
 position
+.lower()
 %0A       
@@ -3015,24 +3015,90 @@
 urn player%0A%0A
+    # TODO:%0A    # find method using position and alternate names%0A%0A
     def __st

Update function to find by name and position to use lower case search and comparison strings