<?php

namespace domain\PointSale\PointSale;

use common\helpers\GlobalParam;
use domain\Distribution\Distribution\Distribution;
use domain\_\AbstractService;
use domain\_\SolverInterface;
use yii\helpers\Html;

class PointSaleSolver extends AbstractService implements SolverInterface
{
    public function filterUserPointSalesByDistribution(array $userPointSaleArray, Distribution $distribution): array
    {
        foreach($userPointSaleArray as $key => $userPointSale) {
            if(!$this->isDelivered($userPointSale->pointSale, $distribution)) {
                unset($userPointSaleArray[$key]);
            }
        }

        return $userPointSaleArray;
    }

    public function isDelivered(PointSale $pointSale, Distribution $distribution): bool
    {
        foreach($distribution->pointSaleDistribution as $pointSaleDistribution) {
            if($pointSaleDistribution->pointSale->id == $pointSale->id && $pointSaleDistribution->delivery) {
                return true;
            }
        }

        return false;
    }

    /**
     * Retourne le commentaire de l'utilisateur courant lié au point de vente.
     */
    public function getComment(PointSale $pointSale): ?string
    {
        if (isset($pointSale->userPointSale)) {
            foreach ($pointSale->userPointSale as $userPointSale) {
                if ($userPointSale->id_user == GlobalParam::getCurrentUserId()) {
                    return $userPointSale->comment;
                }
            }
        }

        return null;
    }

    /**
     * Vérifie le code d'accès à un point de vente.
     */
    public function validateCode(PointSale $pointSale, string $code): bool
    {
        if (strlen($pointSale->code)) {
            if (trim(strtolower($code)) == trim(strtolower($pointSale->code))) {
                return true;
            }
            else {
                return false;
            }
        }

        return true;
    }

    /**
     * Retourne les jours de livraison du point de vente sous forme d'une chaine
     * de caractères.
     */
    public function getStrDeliveryDays(PointSale $pointSale): string
    {
        $str = '';

        if ($pointSale->delivery_monday) $str .= 'lundi, ';
        if ($pointSale->delivery_tuesday) $str .= 'mardi, ';
        if ($pointSale->delivery_wednesday) $str .= 'mercredi, ';
        if ($pointSale->delivery_thursday) $str .= 'jeudi, ';
        if ($pointSale->delivery_friday) $str .= 'vendredi, ';
        if ($pointSale->delivery_saturday) $str .= 'samedi, ';
        if ($pointSale->delivery_sunday) $str .= 'dimanche, ';

        if (strlen($str)) {
            return substr($str, 0, strlen($str) - 2);
        } else {
            return '';
        }
    }

    /**
     * Retourne un commentaire informant l'utilisateur sur les détails de
     * livraison d'un point de vente et pour un jour donné.
     */
    public function getStrInfos(PointSale $pointSale, string $day): string
    {
        $str = '';
        $field = 'infos_' . $day;

        if (strlen($pointSale->$field)) {
            $str = nl2br(Html::encode($pointSale->$field));
            $str = preg_replace('/\[select_previous_day\](.*?)\[\/select_previous_day\]/', '<a href="javascript:void(0);" class="select-previous-day">$1</a>', $str);
            $str = preg_replace('@([^>"])(https?://[a-z0-9\./+,%#_-]+)@i', '$1<a href="$2" target="_blank">$2</a>', $str);
        }
        return $str;
    }

    public function getStrInfosByDistribution(PointSale $pointSale, Distribution $distribution): string
    {
        return $this->getStrInfos($pointSale, strtolower(date('l', strtotime($distribution->date))));
    }

    public function getLocalityWithAddressTooltip(PointSale $pointSale): string
    {
        $html = '<span class="locality">';
        if($pointSale->address && strlen($pointSale->address) > 0) {
            $html .= '<span data-bs-toggle="tooltip" data-bs-placement="bottom" title="'.Html::encode($pointSale->address).'">'.Html::encode($pointSale->locality).'</span>';
        }
        else {
            $html .= Html::encode($pointSale->locality);
        }
        $html .= '</span>';

        return $html;
    }

    public function isPublic(PointSale $pointSale): bool
    {
        if($pointSale->restricted_access || ($pointSale->code && strlen($pointSale->code))) {
            return false;
        }

        return true;
    }
}