# This file is part of Presik.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

from trytond.model import ModelView, ModelSQL, fields, Workflow
# from trytond.pyson import Eval, Bool
# from trytond.pool import Pool


class RatePlan(Workflow, ModelSQL, ModelView):
    'Hotel Rate Plan'
    __name__ = 'hotel.rate_plan'
    price_list = fields.Many2One('product.price_list', 'Price List',
            required=True)
    start_date = fields.Date('Start Date', required=True)
    end_date = fields.Date('End Date', required=True)
    state = fields.Selection([
            ('draft', 'Draft'),
            ('confirmed', 'Confirmed'),
            ('in_progress', 'In progress'),
            ('finished', 'Finished'),
            ('cancelled', 'Canceled'),
        ], 'State', readonly=True)

    @classmethod
    def __setup__(cls):
        super(RatePlan, cls).__setup__()
        # cls._transitions |= set((
        #     ('draft', 'confirmed'),
        #     ('draft', 'cancelled'),
        #     ('confirmed', 'draft'),
        #     ('confirmed', 'in_progress'),
        #     ('cancelled', 'draft'),
        #     ('confirmed', 'cancelled'),
        #     ('in_progress', 'finished'),
        #     ('finished', 'in_progress'),
        # ))
        # cls._buttons.update({
        #     'confirm': {
        #         'invisible': Eval('state') != 'draft',
        #     },
        #     'in_progress': {
        #         'invisible': Eval('state') != 'confirmed',
        #     },
        #     'finish': {
        #         'invisible': Eval('state') != 'in_progress',
        #     },
        #     'draft': {
        #         'invisible': Eval('state').in_(['draft', 'cancelled']),
        #     },
        #     'cancel': {
        #         'invisible': Eval('state') == 'finished',
        #     }
        # })

    @staticmethod
    def default_state():
        return 'draft'

    # @classmethod
    # @ModelView.button
    # @Workflow.transition('draft')
    # def draft(cls, records):
    #     pass
    #
    # @classmethod
    # @ModelView.button
    # @Workflow.transition('cancelled')
    # def cancel(cls, records):
    #     pass
    #
    # @classmethod
    # @ModelView.button
    # @Workflow.transition('confirmed')
    # def confirm(cls, records):
    #     pass
    #
    # @classmethod
    # @ModelView.button
    # @Workflow.transition('in_progress')
    # def in_progress(cls, records):
    #     pass
    #
    # @classmethod
    # @ModelView.button
    # @Workflow.transition('finished')
    # def finish(cls, records):
    #     pass

    @fields.depends('start_date', 'end_date')
    def on_change_start_date(self):
        if not self.start_date:
            self.end_date = None

    def get_days(self, name):
        if self.end_date and self.start_date:
            return (self.end_date - self.start_date).days

    def get_rec_name(self, name):
        return self.price_list.name


class RatePlanRule(ModelSQL, ModelView):
    'Hotel Rate Plan'
    __name__ = 'hotel.rate_plan.rule'
