#   Eve W-Space
#   Copyright 2014 Andrew Austin and contributors
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
from django.db import models
from django.conf import settings
from Map.models import Map, System, MapSystem
from core.utils import get_config
from datetime import datetime
import pytz

# Create your models here.

User = settings.AUTH_USER_MODEL

class Fleet(models.Model):
    """Represents a SiteTracker fleet."""
    system = models.ForeignKey(System, related_name="stfleets")
    initial_boss = models.ForeignKey(User, related_name="bossfleets")
    current_boss = models.ForeignKey(User, related_name="currently_bossing")
    started = models.DateTimeField(auto_now_add=True)
    ended = models.DateTimeField(blank=True, null=True)
    roles_needed = models.ManyToManyField('SiteRole', related_name="fleets_need")

    class Meta:
        permissions = (("can_sitetracker", "Use the Site Tracker system."),)

    def __unicode__(self):
        return u"MapSystem: %s Boss: %s  Started: %s  Ended: %s" % (self.system.name,
                self.current_boss.username, self.started, self.ended)

    def __contains__(self, user):
        """
        Allow for user in fleet syntax to determine both inactive and active
        user records for a fleet.
        """
        if user is None:
            return False
        return self.members.filter(user=user).exists()

    def __iter__(self):
        """
        Provide support for syntax: for User in fleet
        """
        for member_rec in self.members.all():
            yield member_rec.user

    def credit_site(self, site_type, system, boss):