<?php

namespace domain\Producer\Producer;

use domain\_\AbstractGenerator;
use domain\PointSale\PointSale\PointSaleRepository;
use domain\Product\Product\ProductRepository;
use yii\helpers\Html;

class ProducerSeoGenerator extends AbstractGenerator
{
    protected ProductRepository $productRepository;
    protected PointSaleRepository $pointSaleRepository;

    public function loadDependencies(): void
    {
        $this->productRepository = $this->loadService(ProductRepository::class);
        $this->pointSaleRepository = $this->loadService(PointSaleRepository::class);
    }

    public function limitMetaDescriptionLength(string $metaDescription): string
    {
        $length = 160;
        if(strlen($metaDescription) > $length) {
            return substr(strip_tags($metaDescription), 0, $length) . ' ...';
        }

        return $metaDescription;
    }

    public function generateMetaDescriptionHome(): string
    {
        $producer = $this->getProducerContext();

        if($producer->description) {
            return $this->limitMetaDescriptionLength($producer->description);
        }
        else {
            $metaDescription = "Bienvenue sur la boutique du producteur ".Html::encode($producer->name);
        }

        return $metaDescription;
    }

    public function generateMetaDescriptionProducts(): string
    {
        $metaDescription = '';
        $productsArray = $this->productRepository->findProducts();

        foreach($productsArray as $product) {
            $metaDescription .= Html::encode($product->name).', ';
        }

        if(strlen($metaDescription)) {
            $metaDescription = substr($metaDescription, 0, strlen($metaDescription) - 2);
        }

        return $this->limitMetaDescriptionLength($metaDescription);
    }

    public function generateMetaDescriptionPointsSale(): string
    {
        $metaDescription = '';
        $pointsSaleArray = $this->pointSaleRepository->findPointSales();

        foreach($pointsSaleArray as $pointSale) {
            $metaDescription .= Html::encode($pointSale->name).', ';
        }

        if(strlen($metaDescription)) {
            $metaDescription = substr($metaDescription, 0, strlen($metaDescription) - 2);
        }

        return $this->limitMetaDescriptionLength($metaDescription);
    }

    public function generateMetaDescriptionOrder(): string
    {
        $producer = $this->getProducerContext();
        return "Passez commande chez ".Html::encode($producer->name)." à la date et au lieu de votre choix.";
    }

    public function generateMetaDescriptionContact(): string
    {
        $producer = $this->getProducerContext();
        return 'Contactez ce producteur en utilisant le formulaire de contact. '
            .Html::encode($producer->name.', '.$producer->address.' '.$producer->postcode.' '.$producer->city);
    }

    public function generateMetaDescriptionSupport()
    {
        return 'Vous rencontrez un problème ? Contactez le développeur du logiciel en utilisant ce formulaire de contact pour poser vos questions ou faire remonter un bug.';
    }
}