# -*- coding: utf-8 -*-
from odoo import models, fields, api


class Attendance(models.Model):
    _inherit = 'attendance.attendance'

    device_id = fields.Many2one('attendance.device', string='Device', readonly=True,
                                help='The device with which user took check in/out action')
    attendance_id = fields.Many2one('hr.attendance', string='Attendance')


class AttendanceTransaction(models.Model):
    _inherit = 'hr.attendance.transaction'

    checkin_device_id = fields.Many2one('attendance.device', string='Checkin Device', readonly=True,
                                        help='The device with which user took check in action')
    checkout_device_id = fields.Many2one('attendance.device', string='Checkout Device', readonly=True,
                                         help='The device with which user took check out action')


class HrAttendance(models.Model):
    _inherit = 'hr.attendance'

    @api.model_create_multi
    def create(self, vals_list):
        records = super(HrAttendance, self).create(vals_list)
        for res_id in records:
            val = {'employee_id': res_id.employee_id.id,
                   'action_type': 'finger_print',
                   'attendance_id': res_id.id,
                   'device_id': res_id.checkin_device_id and res_id.checkin_device_id.id or False
                   }
            if res_id.check_in:
                val['name'] = res_id.check_in
                val['action'] = 'sign_in'
                val['action_date'] = fields.Datetime.to_datetime(res_id.check_in).date() if res_id.check_in else False
                if val['action_date']:
                    self.env['attendance.attendance'].create(val)
            if res_id.check_out:
                val['name'] = res_id.check_out
                val['action'] = 'sign_out'
                val['device_id'] = res_id.checkout_device_id and res_id.checkout_device_id.id
                val['action_date'] = fields.Datetime.to_datetime(res_id.check_out).date() if res_id.check_out else False
                if val['action_date']:
                    self.env['attendance.attendance'].create(val)
        return records

    def write(self, vals):
        super(HrAttendance, self).write(vals)
        if 'check_out' in vals or 'checkout_device_id' in vals:
            for rec in self:
                if rec.check_out:
                    action_date = fields.Datetime.to_datetime(rec.check_out).date() if rec.check_out else False
                    if action_date:
                        self.env['attendance.attendance'].create({
                            'employee_id': rec.employee_id.id,
                            'action_type': 'finger_print',
                            'attendance_id': rec.id,
                            'name': rec.check_out,
                            'action': 'sign_out',
                            'action_date': action_date,
                            'device_id': rec.checkout_device_id and rec.checkout_device_id.id or False
                        })
