<?php

namespace common\logic\User\CreditHistory\Service;

use common\helpers\MeanPayment;
use common\logic\AbstractService;
use common\logic\SolverInterface;
use common\logic\User\CreditHistory\Model\CreditHistory;
use yii\helpers\Html;

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

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

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

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

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

        if (CreditHistory::TYPE_PAYMENT == $type || CreditHistory::TYPE_REFUND == $type) {
            $order = $creditHistory->getOrderObject();
            if ($order && $order->distribution) {
                $str .= '<br />Commande : ' . date('d/m/Y', strtotime($order->distribution->date));
            } else {
                $str .= '<br />Commande supprimée';
            }
        }

        return $str;
    }

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

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

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

    // strUserAction
    public function getStrUserAction(CreditHistory $creditHistory): string
    {
        $userAction = $creditHistory->getUserActionObject();

        if ($userAction) {
            return $userAction->getName() . ' ' . $userAction->getlastname();
        } else {
            return 'Système';
        }
    }
}