"""Track dispatcher configurations."""
import logging

from django.db import models

log = logging.getLogger('dispatch.models')


class Dispatcher(models.Model):
    """Organize dispatchers by key."""

    key = models.CharField(max_length=16, unique=True)

    bot_xmlrpc_host = models.CharField(max_length=200, default='localhost')
    bot_xmlrpc_port = models.PositiveSmallIntegerField(default=13132)

    class Meta:
        """Meta options."""

        permissions = (
            ('send_message', "Can send messages to dispatchers"),
        )

    def __str__(self):
        """Provide string representation."""
        return "{0:s}".format(self.key)


class DispatcherAction(models.Model):
    """Handle requests to dispatchers and do something with them."""

    PRIVMSG_TYPE = 'privmsg'
    FILE_TYPE = 'file'

    TYPE_CHOICES = (
        (PRIVMSG_TYPE, "IRC privmsg"),
        (FILE_TYPE, "Write to file"),
    )

    dispatcher = models.ForeignKey('Dispatcher', related_name='actions', on_delete=models.CASCADE)
    type = models.CharField(max_length=16, choices=TYPE_CHOICES)
    destination = models.CharField(max_length=200)
    include_key = models.BooleanField(default=False)

    def __str__(self):
        """Provide string representation."""
        return "{0:s} -> {1:s} {2:s}".format(self.dispatcher.key, self.type, self.destination)