<?php

namespace domain\Payment;

use common\helpers\MeanPayment;
use domain\Order\Order\Order;
use domain\_\AbstractService;
use domain\_\SolverInterface;
use domain\User\User\UserSolver;

class PaymentSolver extends AbstractService implements SolverInterface
{
    public function isTypeDebit(Payment $payment): bool
    {
        return in_array($payment->getType(), [
            Payment::TYPE_DEBIT,
            Payment::TYPE_PAYMENT,
        ]);
    }

    public function isTypeCredit(Payment $payment): bool
    {
        return in_array($payment->getType(), [
            Payment::TYPE_CREDIT,
            Payment::TYPE_INITIAL_CREDIT,
            Payment::TYPE_REFUND
        ]);
    }

    public function sumAmountPaid(Payment $payment, float $amount = 0)
    {
        if($this->isTypeDebit($payment)) {
            $amount += $payment->amount;
        }
        else {
            $amount -= $payment->amount;
        }

        return $amount;
    }

    public function getAmount(Payment $payment, bool $format = false): string
    {
        if ($format) {
            return number_format($payment->getAmount(), 2, ',', ' ') . '&nbsp;€';
        } else {
            return $payment->getAmount();
        }
    }

    /**
     * Retourne le libellé du CreditHistory informant de son type et
     * éventuellement de la date de sa commande associée.
     *
     */
    public function getStrWording(Payment $payment, Order $order = null): string
    {
        $str = '';
        $type = $payment->getType();

        if (Payment::TYPE_INITIAL_CREDIT == $type) {
            $str = 'Crédit initial cagnotte';
        } elseif (Payment::TYPE_CREDIT == $type) {
            $str = 'Crédit cagnotte';
        } elseif (Payment::TYPE_PAYMENT == $type) {
            $str = 'Paiement';
        } elseif (Payment::TYPE_REFUND == $type) {
            $str = 'Remboursement';
        } elseif (Payment::TYPE_DEBIT == $type) {
            $str = 'Débit cagnotte';
        }

        if (Payment::TYPE_PAYMENT == $type || Payment::TYPE_REFUND == $type) {

            // Optimisation
            if(!$order) {
                $order = $payment->order;
            }

            if ($order && $order->distribution) {
                //$str .= '<br /><small>commande du <a href="'.\Yii::$app->urlManager->createUrl(['distribution/index', 'date' => $order->distribution->date]).'">' . date('d/m/Y', strtotime($order->distribution->date)).'</a></small>';
                $str .= '<br /><small>Commande du ' . date('d/m/Y', strtotime($order->distribution->date)).'</small>';
            } else {
                $str .= '<br /><small>Commande supprimée</small>';
            }
        }

        return $str;
    }

    public function getDate(Payment $payment, bool $format = false): string
    {
        $date = $payment->getDate();

        if ($format) {
            return date('d/m/Y à H:i:s', strtotime($date));
        } else {
            return $date;
        }
    }

    public function isMeanPaymentCredit(Payment $payment): bool
    {
        return $payment->mean_payment == MeanPayment::CREDIT;
    }

    public function isMeanPayment(Payment $payment, string $meanPayment): bool
    {
        return $payment->mean_payment == $meanPayment;
    }

    public function getStrMeanPayment(Payment $payment): string
    {
        return MeanPayment::getStrBy($payment->getMeanPayment());
    }

    public function getStrType(Payment $payment): string
    {
        switch($payment->type) {
            case Payment::TYPE_PAYMENT:
                return 'Paiement';
            case Payment::TYPE_DEBIT:
                return 'Débit';
            case Payment::TYPE_CREDIT:
                return 'Crédit';
            case Payment::TYPE_REFUND:
                return 'Remboursement';
            case Payment::TYPE_INITIAL_CREDIT:
                return 'Crédit initial';
        }

        return 'Type de paiement inconnu';
    }
}