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

from odoo import models, fields, api, _
from datetime import datetime, timedelta
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT


class EntityRefusedReasonWizard(models.TransientModel):
    _name = 'entity.refused.reason.wizard'

    _description = "Refused Reason Wizard"

    def _default_entity(self):
        return self._context.get('active_id')

    def _default_state(self):
        return self._context.get('state')

    entity_id = fields.Many2one("grant.benefit", string="Benefit", default=_default_entity)
    refused_reason = fields.Text(string='Notes', required=True)
    suspend_reason_id = fields.Many2one("suspend.reason", string="Refused Reason",domain=[('is_reject_reason', '=', True)])
    state = fields.Selection([
        ('draft', 'Draft'),
        ('first_refusal', 'First Refusal'),
        ('approve', 'Approved'),
        ('record_end_date', 'Record end date'),
        ('refused', 'Refused'),
        ('black_list', 'Black List'),
    ], string='state', default=_default_state)

    # @api.multi
    def create_action(self):
        """Throw pop up to write the refusal reason for entity"""
        partner_ids = []
        for rec in self:
            if rec.entity_id:
                # if rec.entity_id.state == 'draft':
                self.env['entity.refuse_reason'].sudo().create(
                    {
                        'name': rec.refused_reason,
                        'entity_id': rec.entity_id.id,
                        'user_id': self.env.uid
                    }
                )
                state = "first_refusal"
                subject = _('Benefit')
                state_label = dict(rec.fields_get(allfields=['state'])['state']['selection'])[state]
                body = _(
                    "The Benefit <b>%s</b> state changed to <b>%s</b>.<br/>"
                    "Suspend Reason: <b>%s</b><br/>"
                    "Notes: <b>%s</b>"
                ) % (rec.entity_id.name,state_label,rec.suspend_reason_id.name if rec.suspend_reason_id else '',rec.refused_reason)
                partner_ids += [(6, 0, rec.entity_id.message_follower_ids.ids)]
                now = fields.datetime.now()
                first_refuse_date = datetime.strftime(now, '%Y-%m-%d')
                rec.entity_id.message_post(body=body, subject=subject, message_type='email')
                rec.entity_id.sudo().write({
                    "state": 'first_refusal',
                    "first_refusal_reason": rec.refused_reason,
                    "first_refuse_date": first_refuse_date,
                    "family_return_reason": rec.refused_reason
                })
        return {'type': 'ir.actions.act_window_close'}

    # @api.multi
    def final_refuse(self):
        """Throw pop up to write the refusal reason for entity"""
        partner_ids = []
        for rec in self:
            if rec.entity_id:
                state = "refused"
                user = self.env['res.users'].search([('partner_id', '=', rec.entity_id.partner_id.id)], limit=1)

                refuse_reason = self.env['entity.refuse_reason'].sudo().create(
                    {
                        'name': rec.refused_reason,
                        'entity_id': rec.entity_id.id,
                        'user_id': self.env.uid,
                    }
                )
                subject = _('Benefit')
                state_label = dict(rec.fields_get(allfields=['state'])['state']['selection'])[state]
                body = ' '.join(
                    (
                        _(u'The Benefit record '), rec.entity_id.name, _(u' State changed to '), state_label,
                        u'.')).encode(
                    'utf-8')
                partner_ids += [(6, 0, rec.entity_id.message_follower_ids.ids)]
                message_vals = {
                    'subject': subject,
                    'body': body,
                    'partner_ids': partner_ids,
                }
                rec.entity_id.message_post(body=body, subject=subject, message_type='email')
                # rec.entity_id.sudo().unlink()
                if user:
                    user.sudo().unlink()
                # rec.entity_id.partner_id.sudo().unlink()
                result = rec.entity_id.sudo().write({
                    "state": 'refused',
                    "final_refusal_reason": rec.refused_reason,
                })
                return result
                    # return {
                    #     'name': _(u'Benefit To Accept'),
                    #     'view_mode': 'tree,form',
                    #     'views': [(self.env.ref('odex_benefit.grant_benefit_tree').id, 'tree'),(self.env.ref('odex_benefit.grant_benefit_form').id, 'form')],
                    #     'res_model': 'grant.benefit',
                    #     'type': 'ir.actions.act_window',
                    #     'target': 'main',
                    # }

class EntityReturnReasonWizard(models.TransientModel):
    _name = 'entity.return.reason.wizard'
    _description = "Return Reason Wizard"

    def _default_entity(self):
        return self._context.get('active_id')

    def _get_target_state(self):
        return self._context.get('target_state', 'complete_info')

    entity_id = fields.Many2one("grant.benefit", string="Benefit", default=_default_entity)
    suspend_reason_id = fields.Many2one("suspend.reason", string="Return Reason", required=True)
    return_reason = fields.Text(string='Notes', required=True)
    target_state = fields.Char(string='Target State', default=_get_target_state)

    def action_confirm_return(self):
        for rec in self:
            if rec.entity_id:
                target_state = rec.target_state
                state_label = dict(rec.entity_id.fields_get(['state'])['state']['selection']).get(target_state,
                                                                                                  target_state)

                subject = _('Benefit')
                body = _(
                    "The benefit record <b>%s</b> state has been changed to <b>%s</b>.<br/>"
                    "Return Reason: <b>%s</b><br/>"
                    "Notes: <b>%s</b>") % (rec.entity_id.name,state_label,rec.suspend_reason_id.name or '',rec.return_reason or '')

                rec.entity_id.message_post(
                    body=body,
                    subject=subject,
                    message_type='notification'
                )

                rec.entity_id.sudo().write({"state": target_state,"family_return_reason": rec.return_reason})
                if rec.entity_id.state == 'draft':
                    rec.entity_id.sudo().write({"family_edit": True})
                    message = rec.entity_id.create_message(target_state)
                    rec.entity_id.user_id.send_sms_to_user(message, rec.entity_id.sms_phone)

                return {'type': 'ir.actions.act_window_close'}

    @api.onchange('target_state')
    def _onchange_target_state(self):
        domain = []
        if self.target_state == "draft":
            domain = [('is_family_return_reason', '=', True)]
        elif self.target_state == "complete_info":
            domain = [('is_return_reason', '=', True)]
        return {'domain': {'suspend_reason_id': domain}}