<?php

// Documentation :

// gestion de la colonne ordre
// https://labs.madisoft.it/avoiding-uniqueconstraintviolationexception-due-to-doctrine-commit-order/


namespace App\Entity\Trait;

use Doctrine\ORM\Mapping as ORM;

#[ORM\HasLifecycleCallbacks]
trait UniqueOrderTrait
{
    function getColumnOrdre() : string {
        return "ordre";
    }

    function getColumnEnabledOrdre() : string {
        return "enabledOrdreUniqueKey";
    }

    function getColumnNewOrdreDirection() : string {
        return "newOrdreDirection";
    }

    function getDefaultEnabledOrdre() : bool {
        return true;
    }

    private $DEFAULT_NEW_ORDER_VALUE = 99999999;
    private $ORDER_DIRECTION_DOWN = 2;
    private $ORDER_DIRECTION_NONE = 1;
    private $ORDER_DIRECTION_UP = 0;
   
    #[ORM\Column(nullable: false)]
    private ?int $ordre = null;

    #[ORM\Column(name: "enabledOrdreUniqueKey", nullable: true)]
    private ?bool $enabledOrdreUniqueKey = null;

    #[ORM\Column(name: "newOrdreDirection", nullable: true)]
    private ?int $newOrdreDirection = null;

    #[ORM\Column(name: "ordreIsLast", nullable: false, options: ["default" => false])]
    private ?bool $isLast = null;

    public function getOrdre(): ?int
    {
        return $this->ordre;
    }

    public function setOrdre(int $ordre): static
    {
        $this->__real_setOrdre($ordre, $this->ORDER_DIRECTION_NONE);
        return $this;
    }

    private function __real_setOrdre(int $ordre, int $ordreDirection): static
    {
        if ($this->ordre == null || $this->ordre != $ordre) {
            $this->enabledOrdreUniqueKey = null;
            $this->newOrdreDirection = $ordreDirection;
        }
        $this->ordre = $ordre;

        // dump($this);

        return $this;
    }
    
    public function setOrdreDown(): static
    {
        if ($this->ordre != null) {
            $this->__real_setOrdre($this->getOrdre() + 1, $this->ORDER_DIRECTION_DOWN);
        } else {
            $this->__real_setOrdre($this->DEFAULT_NEW_ORDER_VALUE, $this->ORDER_DIRECTION_NONE);
        }

        return $this;
    }

    public function setOrdreUp(): static
    {
        if ($this->ordre != null) {
            $this->__real_setOrdre($this->getOrdre() - 1, $this->ORDER_DIRECTION_UP);
        } else {
            $this->__real_setOrdre($this->DEFAULT_NEW_ORDER_VALUE, $this->ORDER_DIRECTION_NONE);
        }

        return $this;
    }
    
    #[ORM\PrePersist]
    public function prePersistOrdre(): void
    {
        if ($this->getOrdre() === null) {
            $this->setOrdre($this->DEFAULT_NEW_ORDER_VALUE);
        }
    }    

    
    public function disableOrdreUniqueKey(): void
    {
        $this->enabledOrdreUniqueKey = null;
    }
 
    public function enableOrdreUniqueKey(): void
    {
        $this->enabledOrdreUniqueKey = true;
        $this->newOrdreDirection = null;
    }

    public function isEnabledOrdreUniqueKey(): ?bool
    {
        return $this->enabledOrdreUniqueKey;
    }

    public function setEnabledOrdreUniqueKey(?bool $enabledOrdreUniqueKey): static
    {
        $this->enabledOrdreUniqueKey = $enabledOrdreUniqueKey;
        return $this;
    }

    public function getNewOrdreDirection(): ?int
    {
        return $this->newOrdreDirection;
    }

    public function setNewOrdreDirection(?int $newOrdreDirection): static
    {
        $this->newOrdreDirection = $newOrdreDirection;
        return $this;
    }

    
    function isFirst() : bool {
        return ($this->ordre == 1);
    }

    #[ORM\PrePersist]
    public function prePersistOrdreIsLast(): void
    {
        if ($this->isLast === null) {
            $this->isLast = false;
        }
    }    

    function isLast() : bool {
        return ($this->isLast != null ? $this->isLast : false);
    }

}
