from .svc_pb2 import Packet, MONITOR
from .zeromq import sender


def notify_mons_service_created(svc, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.create_resp.success = is_success
    rep.create_resp.service.id = svc.id
    rep.create_resp.service.client_id = svc.client_id
    rep.create_resp.service.name = svc.name
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_service_removed(id, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.remove_resp.success = is_success
    rep.remove_resp.service.id = id
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_dvr_created(dvr, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.create_resp.success = is_success
    rep.create_resp.dvr.id = dvr.id
    rep.create_resp.dvr.name = dvr.name
    rep.create_resp.dvr.ip = dvr.ip
    rep.create_resp.dvr.user = dvr.user
    rep.create_resp.dvr.password = dvr.password
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_dvr_removed(id, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.remove_resp.success = is_success
    rep.remove_resp.dvr.id = id
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_stream_created(stm, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.create_resp.success = is_success
    rep.create_resp.stream.id = stm.id
    rep.create_resp.stream.name = stm.name
    rep.create_resp.stream.dvr_id = stm.dvr_id
    rep.create_resp.stream.in_port = stm.in_port
    rep.create_resp.stream.out_port = stm.out_port
    rep.create_resp.stream.channels = stm.channels
    rep.create_resp.stream.type = stm.type
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_stream_removed(id, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.remove_resp.success = is_success
    rep.remove_resp.stream.id = id
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_io_created(io, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.create_resp.success = is_success
    rep.create_resp.io.id = io.id
    rep.create_resp.io.name = io.name
    rep.create_resp.io.dvr_id = io.dvr_id
    rep.create_resp.io.port = io.port
    rep.create_resp.io.state = io.state
    rep.create_resp.io.enabled = io.enabled
    rep.create_resp.io.events_enabled = io.events_enabled
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_io_removed(id, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.remove_resp.success = is_success
    rep.remove_resp.io.id = id
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_door_created(door, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.create_resp.success = is_success
    rep.create_resp.door.id = door.id
    rep.create_resp.door.name = door.name
    rep.create_resp.door.io_id = door.io_id
    rep.create_resp.door.in_pin = door.in_pin
    rep.create_resp.door.out_pin = door.out_pin
    rep.create_resp.door.delay = door.delay
    rep.create_resp.door.nonc = door.nonc
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_door_removed(id, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.remove_resp.success = is_success
    rep.remove_resp.door.id = id
    sender.broadcast_monitors(rep.SerializeToString())


def notify_mons_assoc_created(svc_id, dvr_ids, is_success=True, mon_id='*'):
    rep = Packet()
    rep.client_id = mon_id
    rep.client_type = MONITOR
    rep.create_resp.success = is_success
    rep.create_resp.service_dvr.service_id = svc_id

    for i in dvr_ids:
        rep.create_resp.service_dvr.dvr_ids.append(i)

    sender.broadcast_monitors(rep.SerializeToString())
