"""Support for Z-Wave door locks."""
import logging

import voluptuous as vol

from homeassistant.components.lock import DOMAIN, LockDevice
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect

from . import ZWaveDeviceEntity, const

_LOGGER = logging.getLogger(__name__)

ATTR_NOTIFICATION = "notification"
ATTR_LOCK_STATUS = "lock_status"
ATTR_CODE_SLOT = "code_slot"
ATTR_USERCODE = "usercode"
CONFIG_ADVANCED = "Advanced"

SERVICE_SET_USERCODE = "set_usercode"
SERVICE_GET_USERCODE = "get_usercode"
SERVICE_CLEAR_USERCODE = "clear_usercode"

POLYCONTROL = 0x10E
DANALOCK_V2_BTZE = 0x2
POLYCONTROL_DANALOCK_V2_BTZE_LOCK = (POLYCONTROL, DANALOCK_V2_BTZE)
WORKAROUND_V2BTZE = 1
WORKAROUND_DEVICE_STATE = 2
WORKAROUND_TRACK_MESSAGE = 4
WORKAROUND_ALARM_TYPE = 8

DEVICE_MAPPINGS = {
    POLYCONTROL_DANALOCK_V2_BTZE_LOCK: WORKAROUND_V2BTZE,
    # Kwikset 914TRL ZW500 99100-078
    (0x0090, 0x440): WORKAROUND_DEVICE_STATE,
    (0x0090, 0x446): WORKAROUND_DEVICE_STATE,
    (0x0090, 0x238): WORKAROUND_DEVICE_STATE,
    # Kwikset 888ZW500-15S Smartcode 888
    (0x0090, 0x541): WORKAROUND_DEVICE_STATE,
    # Yale Locks
    # Yale YRD210, YRD220, YRL220
    (0x0129, 0x0000): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD210, YRD220
    (0x0129, 0x0209): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRL210, YRL220
    (0x0129, 0x0409): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD256
    (0x0129, 0x0600): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD110, YRD120
    (0x0129, 0x0800): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD446
    (0x0129, 0x1000): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRL220
    (0x0129, 0x2132): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    (0x0129, 0x3CAC): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD210, YRD220
    (0x0129, 0xAA00): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD220
    (0x0129, 0xFFFF): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRL256
    (0x0129, 0x0F00): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Yale YRD220 (Older Yale products with incorrect vendor ID)
    (0x0109, 0x0000): WORKAROUND_DEVICE_STATE | WORKAROUND_ALARM_TYPE,
    # Schlage BE469
    (0x003B, 0x5044): WORKAROUND_DEVICE_STATE | WORKAROUND_TRACK_MESSAGE,
    # Schlage FE599NX
    (0x003B, 0x504C): WORKAROUND_DEVICE_STATE,
}

LOCK_NOTIFICATION = {
    "1": "Manual Lock",
    "2": "Manual Unlock",
    "5": "Keypad Lock",
    "6": "Keypad Unlock",
    "11": "Lock Jammed",
    "254": "Unknown Event",
}
NOTIFICATION_RF_LOCK = "3"
NOTIFICATION_RF_UNLOCK = "4"
LOCK_NOTIFICATION[NOTIFICATION_RF_LOCK] = "RF Lock"
LOCK_NOTIFICATION[NOTIFICATION_RF_UNLOCK] = "RF Unlock"

LOCK_ALARM_TYPE = {
    "9": "Deadbolt Jammed",
    "16": "Unlocked by Bluetooth ",
    "18": "Locked with Keypad by user ",
    "19": "Unlocked with Keypad by user ",
    "21": "Manually Locked ",
    "22": "Manually Unlocked ",
    "27": "Auto re-lock",
    "33": "User deleted: ",
    "112": "Master code changed or User added: ",
    "113": "Duplicate Pin-code: ",
    "130": "RF module, power restored",
    "144": "Unlocked by NFC Tag or Card by user ",
    "161": "Tamper Alarm: ",
    "167": "Low Battery",
    "168": "Critical Battery Level",
    "169": "Battery too low to operate",
}
ALARM_RF_LOCK = "24"
ALARM_RF_UNLOCK = "25"
LOCK_ALARM_TYPE[ALARM_RF_LOCK] = "Locked by RF"
LOCK_ALARM_TYPE[ALARM_RF_UNLOCK] = "Unlocked by RF"

MANUAL_LOCK_ALARM_LEVEL = {
    "1": "by Key Cylinder or Inside thumb turn",
    "2": "by Touch function (lock and leave)",
}

TAMPER_ALARM_LEVEL = {"1": "Too many keypresses", "2": "Cover removed"}

LOCK_STATUS = {
    "1": True,
    "2": False,
    "3": True,
    "4": False,
    "5": True,
    "6": False,
    "9": False,
    "18": True,
    "19": False,
    "21": True,
    "22": False,
    "24": True,
    "25": False,
    "27": True,
}

ALARM_TYPE_STD = ["18", "19", "33", "112", "113", "144"]

SET_USERCODE_SCHEMA = vol.Schema(
    {
        vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
        vol.Required(ATTR_CODE_SLOT): vol.Coerce(int),
        vol.Required(ATTR_USERCODE): cv.string,
    }
)

GET_USERCODE_SCHEMA = vol.Schema(
    {
        vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
        vol.Required(ATTR_CODE_SLOT): vol.Coerce(int),
    }
)

CLEAR_USERCODE_SCHEMA = vol.Schema(
    {
        vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
        vol.Required(ATTR_CODE_SLOT): vol.Coerce(int),
    }
)


async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
    """Old method of setting up Z-Wave locks."""
    pass


async def async_setup_entry(hass, config_entry, async_add_entities):
    """Set up Z-Wave Lock from Config Entry."""

    @callback
    def async_add_lock(lock):
        """Add Z-Wave Lock."""
        async_add_entities([lock])

    async_dispatcher_connect(hass, "zwave_new_lock", async_add_lock)

    network = hass.data[const.DATA_NETWORK]

    def set_usercode(service):
        """Set the usercode to index X on the lock."""
        node_id = service.data.get(const.ATTR_NODE_ID)
        lock_node = network.nodes[node_id]
        code_slot = service.data.get(ATTR_CODE_SLOT)
        usercode = service.data.get(ATTR_USERCODE)

        for value in lock_node.get_values(
            class_id=const.COMMAND_CLASS_USER_CODE
        ).values():
            if value.index != code_slot:
                continue
            if len(str(usercode)) < 4:
                _LOGGER.error(
                    "Invalid code provided: (%s) "
                    "usercode must be atleast 4 and at most"
                    " %s digits",
                    usercode,
                    len(value.data),
                )
                break
            value.data = str(usercode)
            break

    def get_usercode(service):