from django.db import models
from polymorphic.models import PolymorphicModel

# from .company import Company
from .worker import Worker
from .offer import Offer
from .order import Order


# Action und erbende Klassen
class Action(PolymorphicModel):
    name = models.CharField(default="random Action", max_length=20)
    actionmaker = models.ForeignKey("Company", default=1, on_delete=models.CASCADE)

    def execute(self):
        pass

    def cancel(self):
        pass

    def addActionToCompany(self):
        self.actionmaker.addAction(self)

    def __str__(self):
        return self.name


class ActionEmploy(Action):
    worker = models.ForeignKey(Worker, default=1, on_delete=models.CASCADE)

    @classmethod
    def create(cls, worker, actionmaker):
        # klassenobject erstellen
        name = "Hiring " + worker.name
        action = cls(name=name, worker=worker, actionmaker=actionmaker)
        action.save()
        # worker von market entferenen und action zu company hinzufügen
        actionmaker.removeFromWorkermarket(worker)
        action.addActionToCompany()
        return action

    def execute(self):
        self.worker.setStatus(1)
        self.worker.save()
        self.actionmaker.workers.add(self.worker)
        # Arbeiter zu Company hinzufügen

    def cancel(self):
        self.actionmaker.addToWorkermarket(self.worker)


class ActionUnemploy(Action):
    worker = models.ForeignKey(Worker, null=True, blank=True, on_delete=models.CASCADE)

    @classmethod
    def create(cls, worker, actionmaker):
        # klassenobject erstellen
        name = "Firing " + worker.name
        action = cls(name=name, worker=worker, actionmaker=actionmaker)
        action.save()
        # workerstatus anpassen und action zu company hinzufügen
        actionmaker.workers.remove(worker)
        action.addActionToCompany()
        return action

    def execute(self):
        pass  # self.worker.delete() hier das delete noch machbar machen

    def cancel(self):
        self.actionmaker.workers.add(self.worker)


class ActionOffer(Action):
    order = models.ForeignKey(Order, default=1, on_delete=models.CASCADE)
    amount = models.IntegerField(default=0)

    @classmethod
    def create(cls, order, amount, actionmaker):
        # klassenobject erstellen
        name = "Offer for " + order.name
        action = cls(
            name=name,
            order=order,
            amount=amount,
            actionmaker=actionmaker,
        )
        action.save()
        # order von market entfernen und action zu company hinzufügen
        action.addActionToCompany()
        return action

    def execute(self):
        self.order.setStatus(1)
        self.order.save()
        offer = Offer.objects.create(
            company=self.actionmaker, amount=self.amount, order=self.order
        )
        self.order.offers.add(offer)
        # Angebot einer Order auf OrderMarket vorlegen

    def cancel(self):
        # bleibt leer actually
        pass


class ActionCancelOrder(Action):
    order = models.ForeignKey(Order, default=1, on_delete=models.CASCADE)

    @classmethod
    def create(cls, order, actionmaker):
        # klassenobject erstellen
        name = "Cancel " + order.name
        action = cls(
            name=name,
            order=order,
            actionmaker=actionmaker,
        )
        action.save()
        # action zu company hinzufügen
        action.addActionToCompany()
        return action

    def execute(self):
        self.actionmaker.calculateCustomerSatisfaction(-30)
        self.order.cancelOrder()
        self.order.delete()

    def cancel(self):
        # bleibt leer actually
        pass


class ActionTrain(Action):
    worker = models.ForeignKey(Worker, null=True, blank=True, on_delete=models.CASCADE)
    trainskill = models.CharField(default="empty", max_length=50)
    cost = models.IntegerField(default=1000)

    @classmethod
    def create(cls, worker, trainskill, actionmaker):
        # klassenobject erstellen
        name = "Train Worker " + worker.name
        action = cls(
            name=name,
            worker=worker,
            trainskill=trainskill,
            actionmaker=actionmaker,
        )
        # worker locken status 3
        worker.setStatus(3)
        action.save()
        # action zu company hinzufügen
        action.addActionToCompany()
        return action

    def execute(self):
        # Arbeiter in bestimmtem Skill verbesser und für eine runde locken
        self.worker.improveAttribute(attribute=self.trainskill, amount=10)
        self.worker.setLockedRounds(1)
        # trainingskosten zahlen
        self.actionmaker.addProfit(-self.cost)

    def cancel(self):
        self.worker.setStatus(1)
