from django.db import models
from apps.am.models import Area
from apps.hrm.models import Employee
from apps.opm.models import Opl, OplCate
from apps.utils.models import BaseModel, CommonADModel, CommonAModel, CommonBModel, CommonBDModel
from apps.system.models import Post, User
from apps.third.models import TDevice
# Create your models here.
LEVEL_CHOICES = (
    (10, '低风险'),
    (20, '一般风险'),
    (30, '较大风险'),
    (40, '重大风险')
)


class EventCate(CommonAModel):
    """
    TN: 事件种类
    """
    EVENT_TRIGGER_CHOICES = (
        (10, '监控'),
        (20, '定位'),
        (30, '其他')
    )
    code = models.CharField('标识', max_length=20)
    name = models.CharField('名称', max_length=20, unique=True)
    priority = models.PositiveSmallIntegerField('优先级', default=1, help_text='1-99')
    origin_level = models.PositiveSmallIntegerField('初始等级', default = 10, choices=LEVEL_CHOICES, help_text=str(LEVEL_CHOICES))
    trigger = models.PositiveSmallIntegerField('触发方式', default=10, choices=EVENT_TRIGGER_CHOICES)
    self_algo = models.BooleanField('识别算法', default=False)
    speaker_on = models.BooleanField('开启喇叭报警', default=True)
    # loop_on = models.BooleanField('开启循环拍照', default=False)
    voice_person = models.PositiveSmallIntegerField('声音选择', default=0, help_text='0女声3男声')
    voice_num = models.PositiveSmallIntegerField('播放次数', default=2)
    speakers = models.ManyToManyField(TDevice, verbose_name='固定喇叭', blank=True, related_name='ec_speakers')
    filter_area_level = models.PositiveSmallIntegerField('固定喇叭区域级别过滤', choices=Area.AREA_LEVEL_CHOICES,
                                                         default=Area.AREA_1)
    handle_minute = models.PositiveSmallIntegerField('处理时间', default=0, help_text='超过处理时间事件状态变为超时未处理,0代表未配置')
    same_allow_minute = models.PositiveSmallIntegerField('允许时间', default=0, help_text='同一对象同事件多长时间内不再触发, 0代表一直触发')
    opl_cates = models.ManyToManyField(OplCate, verbose_name='关联许可证', blank=True)
    is_auto_escalate = models.BooleanField('是否自动升级', default=False)
    unhandled_push_interval = models.PositiveIntegerField('未处理事件的推送间隔(秒)', default=3600)

    def __str__(self):
        return self.name


class AlgoChannel(BaseModel):
    """TN: 算法通道"""
    algo = models.ForeignKey(EventCate, verbose_name='关联算法', on_delete=models.CASCADE, related_name='ac_algo')
    vchannel = models.ForeignKey(TDevice, verbose_name='视频通道', on_delete=models.CASCADE, related_name='ac_vchannel')
    always_on = models.BooleanField("实时开启", default=False)

    class Meta:
        unique_together = ('algo', 'vchannel')


class NotifySetting(CommonADModel):
    """
    TN: 提醒配置
    """
    PUSH_FILTER1_CHOICES = (
        (10, '当事人部门'),
        (20, '当事人部门以上'),
        (30, '属地部门'),
        (40, '属地部门以上')
    )
    event_cate = models.ForeignKey(EventCate, verbose_name='关联事件种类', on_delete=models.CASCADE)
    obj_cate = models.CharField('提醒对象', max_length=20, help_text='post岗位/user用户/variable变量')
    post = models.ForeignKey(Post, verbose_name='提醒岗位',
                             on_delete=models.SET_NULL, null=True, blank=True)
    user = models.ForeignKey(User, verbose_name='提醒用户',
                             on_delete=models.SET_NULL, null=True, blank=True)
    variable = models.CharField('提醒变量', max_length=20, null=True, blank=True,
                                help_text='self(本人)/area_manager(区域负责人)/visit_receptionist(访客接待人)')
    filter_recipient = models.PositiveSmallIntegerField('提醒人员过滤', null=True, blank=True)
    filter_area_level = models.PositiveSmallIntegerField('区域级别过滤')
    sms_enable = models.BooleanField('短信通知', default=False)
    wechat_enable = models.BooleanField('开启微信通知', default=False)
    can_handle = models.BooleanField('是否可处理', default=False)
    sort = models.PositiveIntegerField('排序', default=1)


class Event(CommonBDModel):
    """
    TN: 事件
    """
    EVENT_MARK_CHOICES = (
        (10, '正常'),
        (20, '误报'),
    )
    current_level = models.PositiveSmallIntegerField('当前等级', default=10, choices=LEVEL_CHOICES, help_text=str(LEVEL_CHOICES))
    cates = models.ManyToManyField(EventCate, verbose_name='关联事件种类', through='ecm.eventdo', related_name='event_cates')
    face_img = models.CharField('人脸照', max_length=1000, null=True, blank=True)
    global_img = models.CharField('全景照', max_length=1000, null=True, blank=True)
    global_img_compressed = models.CharField('全景照压缩', max_length=1000, null=True, blank=True)
    area = models.ForeignKey(Area, verbose_name='发生区域', on_delete=models.SET_NULL, null=True, blank=True)
    vchannel = models.ForeignKey(TDevice, verbose_name='抓拍设备', on_delete=models.SET_NULL, null=True, blank=True)
    location = models.JSONField('事件点位坐标', default=dict, null=False, blank=True)
    opl = models.ForeignKey(Opl, verbose_name='关联许可证', on_delete=models.SET_NULL, null=True, blank=True)
    obj_cate = models.CharField('发生对象', max_length=20, help_text='people(人员)/opl(作业)/other(其他)...')
    employee = models.ForeignKey(Employee, verbose_name='当事人',
                                 on_delete=models.SET_NULL, null=True, blank=True)
    msg = models.TextField('事件文本', null=True, blank=True)
    voice_msg = models.TextField('语音文本', null=True, blank=True)
    voice = models.TextField('语音地址', null=True, blank=True)
    is_timeout = models.BooleanField('是否超时', default=False)
    mark = models.PositiveSmallIntegerField('事件标记', default=10, help_text='10(正常)/20(误报)')
    happen_time = models.DateTimeField('事件发生时间', null=True, blank=True)
    handle_time = models.DateTimeField('处理时间', null=True, blank=True)
    handle_user = models.ForeignKey(User, verbose_name='处理人',
                                    on_delete=models.SET_NULL, null=True, blank=True)
    handle_desc = models.TextField('处理描述', null=True, blank=True)
    is_pushed = models.BooleanField('是否已推送', default=False)
    last_ws_pushtime = models.DateTimeField('最近ws推送时间', null=True, blank=True)
    enm_data = models.JSONField('能管数据', default=dict, null=False, blank=True)


class Eventdo(BaseModel):
    """TN: 事件处理记录"""
    cate = models.ForeignKey(EventCate, verbose_name='关联事件种类',
                             on_delete=models.CASCADE, related_name='do_cate')
    event = models.ForeignKey(Event, verbose_name='关联事件',
                              on_delete=models.CASCADE)
    handle_time = models.DateTimeField('处理时间', null=True, blank=True)
    handle_user = models.ForeignKey(User, verbose_name='处理人',
                                    on_delete=models.SET_NULL, null=True, blank=True)
    handle_desc = models.TextField('处理描述', null=True, blank=True)


class Remind(BaseModel):
    """
    TN: 事件提醒表
    """
    event = models.ForeignKey(Event, verbose_name='关联事件',
                              on_delete=models.CASCADE)
    recipient = models.ForeignKey(User, verbose_name='接收人',
                                  on_delete=models.CASCADE)
    notify_setting = models.ForeignKey(NotifySetting, verbose_name='通过哪个配置',
                                       on_delete=models.SET_NULL, null=True, blank=True)
    msg = models.TextField('推送文本', null=True, blank=True)
    is_read = models.BooleanField('站内信已读', default=False)
    can_handle = models.BooleanField('是否可处理', default=False)
