<?php

namespace common\logic\Subscription\Subscription\Service;

use common\logic\AbstractService;
use common\logic\Product\Product\Model\Product;
use common\logic\Product\Product\Service\ProductSolver;
use common\logic\SolverInterface;
use common\logic\Subscription\Subscription\Model\Subscription;
use common\logic\User\User\Service\UserSolver;
use yii\helpers\Html;

class SubscriptionSolver extends AbstractService implements SolverInterface
{
    protected UserSolver $userSolver;
    protected ProductSolver $productSolver;

    public function loadDependencies(): void
    {
        $this->userSolver = $this->loadService(UserSolver::class);
        $this->productSolver = $this->loadService(ProductSolver::class);
    }

    /**
     * Informe s'il existe une commande correspond à l'abonnement courant.
     */
    public function hasOrderAlreadyExist(Subscription $subscription, array $orderArray): bool
    {
        if (is_array($orderArray) && count($orderArray) > 0) {
            foreach ($orderArray as $order) {
                if ((($order->id_user > 0 && $order->id_user == $subscription->id_user) ||
                        (!$order->id_user && $order->username == $subscription->username)) &&
                    $order->id_point_sale == $subscription->id_point_sale) {
                    return true;
                }
            }
        }

        return false;
    }

    public function getUsername(Subscription $subscription): string
    {
        if ($subscription->user) {
            return $this->userSolver->getUsername($subscription->user);
        }

        return $subscription->username;
    }

    /**
     * Valide le fait qu'un abonnement est bien compatible avec une date donnée.
     */
    public function isSubscriptionMatchWith(Subscription $subscription, string $date): bool
    {
        $arrayDays = [
            1 => 'monday',
            2 => 'tuesday',
            3 => 'wednesday',
            4 => 'thursday',
            5 => 'friday',
            6 => 'saturday',
            7 => 'sunday'
        ];

        $nbDays = (strtotime($date) - strtotime($subscription->date_begin)) / (24 * 60 * 60);
        if (round($nbDays) % ($subscription->week_frequency * 7) < 7) {
            $numDay = date('N', strtotime($date));
            $day = $arrayDays[$numDay];
            if ($subscription->$day) {
                return true;
            }
        }

        return false;
    }

    public function getProductsListAsHtml(Subscription $subscription): string
    {
        $html = '' ;
        foreach($subscription->productSubscription as $productSubscription)
        {
            if(isset($productSubscription->product) && $productSubscription->product) {
                $html .= Html::encode($productSubscription->product->name).' ('.($productSubscription->quantity *   Product::$unitsArray[$productSubscription->product->unit]['coefficient']) . '&nbsp'. $this->productSolver->strUnit($productSubscription->product, 'wording_short').')<br />' ;
            }
            else {
                $html .= 'Produit non défini<br />'  ;
            }
        }

        if(!count($subscription->productSubscription))
        {
            $html .= '<span class="glyphicon glyphicon-warning-sign"></span> Aucun produit' ;
        }

        return $html ;
    }

    public function getPeriodAsHtml(Subscription $subscription): string
    {
        $html = '<small>' ;
        if($subscription->date_end) {
            $html .= 'Du&nbsp;' ;
        }
        else {
            $html .= 'À partir du ' ;
        }
        $html .= '</small>' ;
        $html .= date('d/m/Y',strtotime($subscription->date_begin)) ;
        if($subscription->date_end) {
            $html .= '<br />au&nbsp;'.date('d/m/Y',strtotime($subscription->date_end)) ;
            if(date('Y-m-d') > $subscription->date_end) {
                $html .= ' <span class="label label-danger">Terminé</span>' ;
            }
        }

        return $html ;
    }

    public function getDaysAsHtml(Subscription $subscription): string
    {
        $html = '' ;
        if($subscription->monday) {
            $html .= 'lundi, ' ;
        }
        if($subscription->tuesday) {
            $html .= 'mardi, ' ;
        }
        if($subscription->wednesday) {
            $html .= 'mercredi, ' ;
        }
        if($subscription->thursday) {
            $html .= 'jeudi, ' ;
        }
        if($subscription->friday) {
            $html .= 'vendredi, ' ;
        }
        if($subscription->saturday) {
            $html .= 'samedi, ' ;
        }
        if($subscription->sunday) {
            $html .= 'dimanche, ' ;
        }

        if(strlen($html)) {
            return substr ($html, 0, strlen($html) - 2) ;
        }
        else {
            return '<span class="glyphicon glyphicon-warning-sign"></span> Aucun jour' ;
        }
    }
}