#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu

from __future__ import (unicode_literals, absolute_import,
                        division, print_function)
import re
import datetime

from django.db import models
from django.utils.translation import ugettext_lazy as _, ugettext
from django.utils.dateformat import format as date_format
from django.utils import timezone

from snisi_core.models.Periods import MonthPeriod, ONE_MICROSECOND_DELTA
from snisi_tools.datetime import normalize_date


class FixedMonthFirstWeekManager(models.Manager):
    def get_queryset(self):
        return super(FixedMonthFirstWeekManager, self) \
            .get_queryset().filter(period_type=FixedMonthFirstWeek.FWP)


class FixedMonthSecondWeekManager(models.Manager):
    def get_queryset(self):
        return super(FixedMonthSecondWeekManager, self) \
            .get_queryset().filter(period_type=FixedMonthSecondWeek.FWP)


class FixedMonthThirdWeekManager(models.Manager):
    def get_queryset(self):
        return super(FixedMonthThirdWeekManager, self) \
            .get_queryset().filter(period_type=FixedMonthThirdWeek.FWP)


class FixedMonthFourthWeekManager(models.Manager):
    def get_queryset(self):
        return super(FixedMonthFourthWeekManager, self) \
            .get_queryset().filter(period_type=FixedMonthFourthWeek.FWP)


class FixedMonthFifthWeekManager(models.Manager):
    def get_queryset(self):
        return super(FixedMonthFifthWeekManager, self) \
            .get_queryset().filter(period_type=FixedMonthFifthWeek.FWP)


class FixedMonthFirstWeek(MonthPeriod):

    FWP = 'fixed_month_first_week'
    FIXED_WEEK_NUM = 1

    class Meta:
        proxy = True
        app_label = 'snisi_core'
        verbose_name = _("Monthly Reporting Period")
        verbose_name_plural = _("Monthly Reporting Periods")

    objects = FixedMonthFirstWeekManager()

    @classmethod
    def from_url_str(cls, period_str):
        return FixedMonthWeek.from_url_str(period_str)

    @classmethod
    def type(cls):
        return cls.FWP

    @property
    def pid(self):
        return self.middle().strftime('FM1W%m%Y')

    def name(self):
        # Translators: Django's date template format for MonthPeriod.name()
        return date_format(self.middle(), ugettext("1-7 F Y"))

    def full_name(self):
        # Translators: Django's date tmpl format for MonthPeriod.full_name()
        return date_format(self.middle(), ugettext("1st to 7th F Y"))

    @classmethod
    def delta(self):
        return 31

    @classmethod
    def boundaries(cls, date_obj):
        date_obj = normalize_date(date_obj, as_aware=True)
        start = date_obj.replace(day=1, hour=0, minute=0,
                                 second=0, microsecond=0)
        end = start.replace(day=8) - ONE_MICROSECOND_DELTA
        return (start, end)

    def strid(self):
        return self.pid
        return self.middle().strftime('[1-7]-%m-%Y')


class FixedMonthSecondWeek(MonthPeriod):

    FWP = 'fixed_month_second_week'
    FIXED_WEEK_NUM = 2

    class Meta: