"""This module contains Server model class and basic functions"""

from django.db import models

from registration.models import CustomUser

STATE_CHOICES = (
    ('', "Disabled"),
    ('prod', "Production"),
    ('stag', "Staging"),
)


class Server(models.Model):
    """
    Server

    :argument id: int - autogenerated primary key
    :argument name: str - Server name
    :argument address: str - Server address
    :argument state: str - Server state ('NotSelected'/'Production','Staging')
    :argument user: id - CustomUser id - user owner id
    """

    name = models.CharField(max_length=50)
    address = models.CharField(max_length=200)
    state = models.CharField(max_length=20, choices=STATE_CHOICES, default='')
    user = models.ForeignKey(CustomUser, default=1, on_delete=models.CASCADE)

    @staticmethod
    def create(name, address, state, user):
        """Create and add server to database.

        :param name: str - server name
        :param address: str - server address
        :param state: str - server state
        :param user: user
        :return: Created server for success, None otherwise.
        """

        server = Server()
        server.name = name
        server.address = address
        server.state = state
        server.user = user
        server.save()

        return server

    def update(self, name=None, address=None, state=None):
        """Update server data.

        :param name: str - server name
        :param address: str - server address
        :param state: str - server state
        """

        if name:
            self.name = name
        if address:
            self.address = address
        if state:
            self.state = state

        self.save()

    @staticmethod
    def get_by_id(server_id):
        """Get server with given id.

        :param id: int - server id
        :return: Server if server was found, and None otherwise.
        """

        try:
            server = Server.objects.get(id=server_id)
        except Server.DoesNotExist:
            return None

        return server

    @staticmethod
    def get_by_user_id(user_id):
        """
        :param user_id: int - user id
        :return: QuerySet<Server>: QuerySet of servers.
        """

        server = Server.objects.filter(user=user_id)
        return server

    def to_dict(self):
        """Convert model object to dictionary.

        :return: dict:
                {
                    'id': id,
                    'name': name,
                    'address': address.
                    'state': state,
                    'user_id': user.id
                }
        """

        return {
            'id': self.id,
            'name': self.name,
            'address': self.address,
            'state': self.state,
            'user_id': self.user.id
        }

    def __str__(self):
        return "ServerId: {}, ServerName: {}, ServerAddress: {}, " \
               "ServerState {}".format(self.id,
                                       self.name,
                                       self.address,
                                       self.state)
