from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
import random
from .engineeringmodel import EngineeringModel, ModelChaotic
from .attributeset import AttributeSet
from .names import WorkerNames


# worker klasse
class Worker(models.Model):
    name = models.CharField(default="random worker", max_length=20)
    attributes = models.ForeignKey(AttributeSet, default=1, on_delete=models.CASCADE)
    skilledModels = models.ForeignKey(
        EngineeringModel, on_delete=models.CASCADE, blank=True
    )
    amplifyer = models.FloatField(
        default=1, validators=[MinValueValidator(1.0), MaxValueValidator(2.0)]
    )
    salary = models.IntegerField(default=1000)
    status = models.IntegerField(
        default=0, validators=[MinValueValidator(0), MaxValueValidator(3)]
    )
    # status 0 = in workermarket; 1=in company; 2=wokring on order; 3=locked
    lockedRounds = models.IntegerField(default=0, validators=[MinValueValidator(0)])
    happyness = models.IntegerField(
        default=100, validators=[MinValueValidator(0), MaxValueValidator(100)]
    )
    sick = models.BooleanField(default=False)

    def __str__(self):
        return self.name

    @classmethod
    def createRandom(cls):
        name = random.choice(WorkerNames.objects.all()).name
        attributes = AttributeSet.createRandom(20)
        allmodels = EngineeringModel.objects.all()
        allmodelsnochaotic = [
            model for model in allmodels if not isinstance(model, ModelChaotic)
        ]
        skilledModels = random.choice(allmodelsnochaotic)
        if random.random() < 0.8:
            value = round(random.uniform(1, 1.3), 1)
        else:
            value = round(random.uniform(1.3, 1.6), 1)
        amplifyer = value
        salary = round(amplifyer * 2500 * random.uniform(0.7, 1.3))
        worker = cls(
            name=name,
            attributes=attributes,
            skilledModels=skilledModels,
            amplifyer=amplifyer,
            salary=salary,
            status=0,
        )
        worker.save()
        return worker

    def improveAttribute(self, attribute, amount):
        self.attributes.increase(attribute=attribute, amount=amount)

    def calculateHappyness(self, number):
        if (self.happyness + number) > 100:
            self.happyness = 100
        elif (self.happyness + number) < 0:
            self.happyness = 0
        else:
            self.happyness += number
        self.save()

    def quitJob(self):
        self.delete()

    def calculateSick(self):
        if self.sick == False:
            randnumber = random.randint(1, 100)
            if randnumber <= 3:
                self.setStatus(3)
                self.setSick(True)
                rounds = random.randint(1, 2)
                self.setLockedRounds(rounds)
        elif self.sick == True and self.lockedRounds < 0:
            self.setSick(False)
            self.setStatus(1)

    def setStatus(self, status):
        self.status = status
        self.save()

    def getStatus(self):
        return self.status

    def setLockedRounds(self, amount):
        self.lockedRounds = amount
        self.save()

    def setSick(self, sickness):
        self.sick = sickness
        self.save()
