# -*- coding: utf-8 -*-

# Copyright © 2013 Peter Schnebel <pschnebel@gmx.de>

# This file is part of python-ads.
#
# python-ads is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# python-ads is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# python-ads.  If not, see <http://www.gnu.org/licenses/>.

from copy import copy
from helpers import now, scale_rating
from os import sep

from log import Logger

class Artist(object):
  name = None
  track_cnt = 0
  track_rating = 0.0
  rating = 0.5
  test = None # for testing only
  active = True
  lastplayed = 0
  laststarted = 0
  lastqueued = 0
  relation = 0.5
  relation_old = 0.5
  relation_sum = 0.0
  relation_cnt = 0

  def __init__(self, artist_name):
    self.name = artist_name

  def activate(self):
    self.active = True

  def deactivate(self):
    self.active = False

  def queued(self):
    self.lastqueued = now()

  def started(self):
    self.laststarted = now()

  def update(self, diff, new, factor=1.0):
    if diff > 0.0: self.lastplayed = now()
    if new:
      self.track_cnt += 1
      diff += 0.5
    self.track_rating += diff
    if self.track_cnt > 0: self.rating = self.track_rating / self.track_cnt
    Logger.info(u"updated rating: %s" % self)

  def tracks(self):
    return TrackFactory.by_artist(self)

  def files(self):
    return FileFactory.by_artist(self)

  def __str__(self):
    return u'A[{},{},r{}]'.format(self.name, self.track_cnt,
        scale_rating(self.rating))

  def key(self):
    """ return the key for ArtistFactory.by_key() """
    return ArtistFactory.get_key(self.name)

  def __getstate__(self):
    result = copy(self.__dict__)
    if result.has_key('relation'): del result['relation']
    if result.has_key('relation_old'): del result['relation_old']
    if result.has_key('relation_sum'): del result['relation_sum']
    if result.has_key('relation_cnt'): del result['relation_cnt']
    return self.__dict__

  def __setstate__(self, dict):
    # Logger.info(u"artist dict in: %s" % dict)
    self.__dict__ = dict
    # Logger.info(u"artist dict out: %s" % self.__dict__)
    ArtistFactory.register(self)

class ArtistFactory(object):
  __lib = {}

  @staticmethod
  def clear(): ArtistFactory.__lib.clear()

  @staticmethod
  def get_key(artist_name):
    return artist_name.replace(sep, "_")

  @staticmethod
  def get(artist_name):
    key = ArtistFactory.get_key(artist_name)
    if not ArtistFactory.__lib.has_key(key):
      ArtistFactory.__lib[key] = Artist(artist_name)
    return ArtistFactory.__lib[key]

  @staticmethod
  def register(artist): ArtistFactory.__lib[artist.key()] = artist

  @staticmethod
  def by_key(key):
    if ArtistFactory.__lib.has_key(key): return ArtistFactory.__lib[key]
    return None

  @staticmethod
  def getstate(): return ArtistFactory.__lib

  @staticmethod
  def setstate(dict): ArtistFactory.__lib = dict

  @staticmethod
  def active_artists():
    for a in ArtistFactory.__lib.values():
      if a.active: yield a

  @staticmethod
  def len(): return len(ArtistFactory.__lib)

from track import TrackFactory, Track
from file import FileFactory, File

# vim: set ai ts=2 sw=2 sts=2 expandtab:
# eof
