<?php

namespace common\logic\Distribution\Distribution\Service;

use common\helpers\GlobalParam;
use common\logic\AbstractService;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\Product\Product\Model\Product;
use common\logic\SolverInterface;

class DistributionSolver extends AbstractService implements SolverInterface
{
    public function validateDistributionDate(string $date = null): bool
    {
        $format = 'Y-m-d';
        $dateObject = \DateTime::createFromFormat($format, $date);

        if ($dateObject && $dateObject->format($format) === $date) {
            return true;
        }

        return false;
    }

    public function getDistributionDayAsString(Distribution $distribution): string
    {
        $dayDistribution = date('N', strtotime($distribution->date));

        $daysArray = [
            1 => 'monday',
            2 => 'tuesday',
            3 => 'wednesday',
            4 => 'thursday',
            5 => 'friday',
            6 => 'saturday',
            7 => 'sunday',
        ];

        return $daysArray[$dayDistribution];
    }

    /**
     * Déduit si un produit est actif ou non
     */
    public function isActiveProduct(Distribution $distribution, Product $product): bool
    {
        if ($product->id
            && isset($distribution->productDistribution)
            && count($distribution->productDistribution) > 0) {

            foreach ($distribution->productDistribution as $productDistribution) {
                if ($productDistribution['id_product'] == $product->id
                    && $productDistribution['active']) {
                    return true;
                }
            }
        }

        return false;
    }

    // isAvailable
    public function isDistributionAvailable(Distribution $distribution): bool
    {
        $dateToday = date('Y-m-d');
        $producer = $distribution->producer;
        $dateDistribution = $distribution->date;
        $dayDistribution = strtolower(date('l', strtotime($dateDistribution)));
        $fieldDayDistributionDelay = 'order_delay_' . $dayDistribution;
        $fieldDayDistributionDeadline = 'order_deadline_' . $dayDistribution;

        $delay = $producer->order_delay;
        $deadline = $producer->order_deadline;

        if ($producer->$fieldDayDistributionDelay) {
            $delay = $producer->$fieldDayDistributionDelay;
        }

        if ($producer->$fieldDayDistributionDeadline) {
            $deadline = $producer->$fieldDayDistributionDeadline;
        }

        $countDaysTodayDistribution = number_format((strtotime($dateDistribution) - strtotime($dateToday)) / (24 * 60 * 60), 0);

        if(date('H') >= $deadline) {
            $countDaysTodayDistribution--;
        }

        if ($countDaysTodayDistribution < $delay) {
            return false;
        }

        return true;
    }

    /**
     * Filtre les distributions par délai de commande.
     */
    public function filterDistributionsByDateDelay(array $distributionsArray): array
    {
        foreach ($distributionsArray as $keyDistribution => $distribution) {
            if(!$this->isDistributionAvailable($distribution)) {
                unset($distributionsArray[$keyDistribution]);
            }
        }

        $newDistributionsArray = [];
        foreach ($distributionsArray as $distribution) {
            $newDistributionsArray[] = $distribution;
        }

        return $newDistributionsArray;
    }

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

        return false;
    }

    public function getLinkOrder(Distribution $distribution): string
    {
        return \Yii::$app->urlManagerProducer->createAbsoluteUrl([
            'order/order',
            'slug_producer' => $this->getProducerContext()->slug,
            'date' => $distribution->date
        ]) ;
    }
}