<?php

namespace Lc\ShopBundle\Model;

use App\Entity\OrderPayment;
use App\Entity\OrderRefund;
use App\Entity\UserMerchant;
use Doctrine\ORM\Mapping as ORM;
use Lc\ShopBundle\Context\OrderPaymentInterface;
use Lc\ShopBundle\Context\OrderRefundInterface;
use Lc\ShopBundle\Context\PayoffInterface;
use Lc\ShopBundle\Context\UserMerchantInterface;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\MappedSuperclass()
 */
abstract class CreditHistory extends AbstractEntity implements PayoffInterface
{
        use PayoffTrait;

        const TYPE_CREDIT = 'credit';
        const TYPE_DEBIT = 'debit';

        /**
         * @ORM\Column(type="float", nullable=true)
         */
        protected $amount;

        /**
         * @ORM\Column(type="string", length=31)
         */
        protected $type;

        /**
         * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UserMerchantInterface", inversedBy="creditHistories")
         * @ORM\JoinColumn(nullable=false)
         */
        protected $userMerchant;

        /**
         * @ORM\OneToOne(targetEntity="Lc\ShopBundle\Context\OrderPaymentInterface", cascade={"persist", "remove"})
         */
        protected $orderPayment;

        /**
         * @ORM\OneToOne(targetEntity="Lc\ShopBundle\Context\OrderRefundInterface", cascade={"persist", "remove"})
         */
        protected $orderRefund;

        /**
         * @Gedmo\Blameable(on="create")
         * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UserInterface")
         * @ORM\JoinColumn(nullable=true)
         */
        protected $createdBy;

        /**
         * @Gedmo\Blameable(on="update")
         * @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\UserInterface")
         * @ORM\JoinColumn(nullable=true)
         */
        protected $updatedBy;

        public function getAmount(): ?float
        {
                return $this->amount;
        }

        public function setAmount(?float $amount): self
        {
                $this->amount = $amount;

                return $this;
        }

        public function getAmountInherited(): float
        {
                if ($this->getOrderPayment() !== null) {
                        return $this->getOrderPayment()->getAmount();
                } else if ($this->getOrderRefund() !== null) {
                        return $this->getOrderRefund()->getAmount();
                } else {
                        return $this->getAmount();
                }
        }

        public function getMeanPaymentInherited(): string
        {
                if ($this->getOrderPayment() !== null) {
                        return $this->getOrderPayment()->getMeanPayment();
                } else if ($this->getOrderRefund() !== null) {
                        return $this->getOrderRefund()->getMeanPayment();
                } else {
                        return $this->getMeanPayment();
                }
        }

        public function getPaidAtInherited (): ?\DateTimeInterface
        {
                if ($this->getOrderPayment() !== null) {
                        return $this->getOrderPayment()->getPaidAt();
                } else if ($this->getOrderRefund() !== null) {
                        return $this->getOrderRefund()->getPaidAt();
                } else {
                        return $this->getPaidAt();
                }
        }

        public function getReferenceInherited (): ?string
        {
                if ($this->getOrderPayment() !== null) {
                        return $this->getOrderPayment()->getReference();
                } else if ($this->getOrderRefund() !== null) {
                        return $this->getOrderRefund()->getReference();
                } else {
                        return $this->getReference();
                }
        }

        public function getCommentInherited (): ?string
        {
                if ($this->getOrderPayment() !== null) {
                        return $this->getOrderPayment()->getComment();
                } else if ($this->getOrderRefund() !== null) {
                        return $this->getOrderRefund()->getComment();
                } else {
                        return $this->getComment();
                }
        }

        public function getMeanPaymentInheritedLabel(): string
        {
                return 'field.default.meanPaymentOptions.'.$this->getMeanPaymentInherited() ;
        }

        public function getType(): ?string
        {
                return $this->type;
        }

        public function setType(string $type): self
        {
                $this->type = $type;

                return $this;
        }

        public function getUserMerchant(): ?UserMerchantInterface
        {
                return $this->userMerchant;
        }

        public function setUserMerchant(?UserMerchantInterface $userMerchant): self
        {
                $this->userMerchant = $userMerchant;

                return $this;
        }

        public function getOrderPayment(): ?OrderPaymentInterface
        {
                return $this->orderPayment;
        }

        public function setOrderPayment(?OrderPaymentInterface $orderPayment): self
        {
                $this->orderPayment = $orderPayment;

                return $this;
        }

        public function getOrderRefund(): ?OrderRefundInterface
        {
                return $this->orderRefund;
        }

        public function setOrderRefund(?OrderRefundInterface $orderRefund): self
        {
                $this->orderRefund = $orderRefund;

                return $this;
        }
}