from hub import methods, Global


async def code2001(**sources):
    """
    新增车辆
    """
    # --- check ---
    if not sources.get('pid'):
        return dict(code=1, details=f"something is wrong.")
    elif not sources.get('type'):
        return dict(code=2, details=f"something is wrong.")

    # --- check ---
    count = Global.mdb.get_count('VehicleInfo', {'pid': sources.get('pid')})
    if count != 0:
        return dict(code=3, details=f"something is wrong.")

    # --- save ---
    """
    VehicleInfo: 车辆信息表
    VehicleInfo.pid: 车牌号
    VehicleInfo.type: 车型号
    VehicleInfo.host_address: 工控机地址
    VehicleInfo.rtk_address: rtk地址
    VehicleInfo.cpe_address: cpe地址
    VehicleInfo.release_at: 出厂时期
    VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
    VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
    VehicleInfo.update_at: 更新时间
    """
    data = {
        'pid': sources.get('pid'),
        'type': sources.get('type'),
        'host_address': sources.get('host_address'),
        'rtk_address': sources.get('rtk_address'),
        'cpe_address': sources.get('cpe_address'),
        'release_at': sources.get('release_at'),
        'update_at': methods.now_ts(),
        'state': 1,
        'permit_state': 0,
    }
    uuid = Global.mdb.add('VehicleInfo', data)
    return dict(code=0, data=uuid)


async def code2002(**sources):
    """
    查询车辆列表
    """
    # --- check ---
    if not sources.get('page'):
        return dict(code=1, details=f"something is wrong.")
    elif not sources.get('size'):
        return dict(code=2, details=f"something is wrong.")

    # --- fill d1 ---
    d1 = list()
    page = sources.get('page', 1)
    size = sources.get('size', 10)
    for item in Global.mdb.get_all('VehicleInfo', sort_field=[('update_at', -1)]):

        # --- check ---
        if sources.get('pid') and sources.get('pid') not in item.get('pid'):
            continue

        data = {
            'uuid': str(item.get('_id')),
            'pid': item.get('pid'),
            'type': item.get('type'),
            'host_address': item.get('host_address'),
            'rtk_address': item.get('rtk_address'),
            'cpe_address': item.get('cpe_address'),
            'release_at': item.get('release_at'),
            'state': item.get('state', 1),
            'permit_state': item.get('permit_state', 0),
        }
        d1.append(data)
    return dict(code=0, data=d1[(page - 1) * size: page * size], total=len(d1), page=page, size=size)


async def code2003(**sources):
    """
    修改指定车辆信息
    """
    # --- check ---
    uuid = sources.get('uuid')
    if not uuid:
        return dict(code=1, details=f"something is wrong.")

    # --- check ---
    item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
    if not item:
        return dict(code=2, details=f"something is wrong.")

    # --- check ---
    # count = Global.mdb.get_count('VehicleInfo', {'pid': sources.get('pid')})
    # if count != 0:
    #     return dict(code=3, details=f"something is wrong.")

    # --- update ---
    """
    VehicleInfo: 车辆信息表
    VehicleInfo.pid: 车牌号
    VehicleInfo.type: 车型号
    VehicleInfo.host_address: 工控机地址
    VehicleInfo.rtk_address: rtk地址
    VehicleInfo.cpe_address: cpe地址
    VehicleInfo.release_at: 出厂时期
    VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
    VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
    VehicleInfo.update_at: 更新时间
    """
    update_dict = dict()
    update_dict['update_at'] = methods.now_ts()
    if sources.get('pid'):
        update_dict['pid'] = sources.get('pid')
    if sources.get('type'):
        update_dict['type'] = sources.get('type')
    if sources.get('host_address'):
        update_dict['host_address'] = sources.get('host_address')
    if sources.get('rtk_address'):
        update_dict['rtk_address'] = sources.get('rtk_address')
    if sources.get('cpe_address'):
        update_dict['cpe_address'] = sources.get('cpe_address')
    if sources.get('release_at'):
        update_dict['release_at'] = sources.get('release_at')
    Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict)
    return dict(code=0, data=uuid)

async def code2004(**sources):
    """
    禁止指定车辆远程操作
    """
    # --- check ---
    uuid = sources.get('uuid')
    if not uuid:
        return dict(code=1, details=f"something is wrong.")

    # --- check ---
    item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
    if not item:
        return dict(code=2, details=f"something is wrong.")

    # --- update ---
    """
    VehicleInfo: 车辆信息表
    VehicleInfo.pid: 车牌号
    VehicleInfo.type: 车型号
    VehicleInfo.host_address: 工控机地址
    VehicleInfo.rtk_address: rtk地址
    VehicleInfo.cpe_address: cpe地址
    VehicleInfo.release_at: 出厂时期
    VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
    VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
    VehicleInfo.update_at: 更新时间
    """
    update_dict = dict()
    update_dict['permit_state'] = 1
    item = Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict, need_back=True)
    return dict(code=0, data=item)

async def code2005(**sources):
    """
    允许指定车辆远程操作
    """
    # --- check ---
    uuid = sources.get('uuid')
    if not uuid:
        return dict(code=1, details=f"something is wrong.")

    # --- check ---
    item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
    if not item:
        return dict(code=2, details=f"something is wrong.")

    # --- update ---
    """
    VehicleInfo: 车辆信息表
    VehicleInfo.pid: 车牌号
    VehicleInfo.type: 车型号
    VehicleInfo.host_address: 工控机地址
    VehicleInfo.rtk_address: rtk地址
    VehicleInfo.cpe_address: cpe地址
    VehicleInfo.release_at: 出厂时期
    VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
    VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
    VehicleInfo.update_at: 更新时间
    """
    update_dict = dict()
    update_dict['permit_state'] = 0
    item = Global.mdb.update_one_by_id('VehicleInfo', uuid, update_dict, need_back=True)
    return dict(code=0, data=item)

async def code2006(**sources):
    """
    删除指定车辆
    """
    # --- check ---
    uuid = sources.get('uuid')
    if not uuid:
        return dict(code=1, details=f"something is wrong.")

    # --- check ---
    item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
    if not item:
        return dict(code=2, details=f"something is wrong.")

    # --- clean ---
    Global.mdb.remove_one_by_id('VehicleInfo', uuid)
    return dict(code=0, data=uuid)

async def code2007(**sources):
    """
    获取指定作业车辆详情
    """
    uuid = sources.get('uuid')
    if not uuid:
        return dict(code=1, details=f"something is wrong.")

    item = Global.mdb.get_one_by_id('VehicleInfo', uuid)
    if not item:
        return dict(code=2, details=f"something is wrong.")

    # --- fill ---
    """
    VehicleInfo: 车辆信息表
    VehicleInfo.pid: 车牌号
    VehicleInfo.type: 车型号
    VehicleInfo.host_address: 工控机地址
    VehicleInfo.rtk_address: rtk地址
    VehicleInfo.cpe_address: cpe地址
    VehicleInfo.release_at: 出厂时期
    VehicleInfo.state: 当前状态 (1:离线 2:在线空闲 3: 现场驾驶中 4: 远程驾驶中)
    VehicleInfo.permit_state: 遥操状态 (0:允许 1:禁用)
    VehicleInfo.update_at: 更新时间
    """
    item.pop('_id')
    item['uuid'] = uuid
    return dict(code=0, data=item)