<?php

namespace App\Classes\Modules\ServiceTypes\Services;


use App\Classes\General\Helper;
use App\Classes\Modules\Currencies\DataTransferObjects\RateObject;
use App\Classes\Modules\ServiceTypes\DataTransferObjects\ServiceCurrenciesObject;
use App\Classes\Modules\ServiceTypes\DataTransferObjects\ServiceDetailObject;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use Illuminate\Support\Str;

class CreatesServiceTypeConstantDetails
{

    /** @var array */
    private $ConstantDetails;


    /**
     * @param ServiceDetailObject $object
     * @param bool|null $isCustomSegment
     * @return array
     */
    public function execute(ServiceDetailObject $object, ?bool $isCustomSegment = false) {

        foreach (Helper::getClassMethodsArray(ServiceDetailObject::class) as $methodName){

            if(Helper::getPropertyName($methodName) === 'currencies'){

                $this->mapCurrencies($object->$methodName(), $isCustomSegment);

                continue;
            }

            $this->addToConstantDetails($methodName, $this->cleanValue($object->$methodName()));

        }

        return $this->ConstantDetails;

    }

    /**
     * @param array $currencies
     * @param bool $isCustomSegment
     */
    private function mapCurrencies(array $currencies, bool $isCustomSegment){

        $isEmpty = true;
        foreach ($currencies as $currency){

            if(!$currency->isActive()) { continue; }

            $currencyObject = [];


            foreach (Helper::getClassMethodsArray(ServiceCurrenciesObject::class) as $methodName){

                if($methodName === 'isActive') continue;
                if($methodName === 'getRates') {
                    $currencyObject['rates'] = $this->mapRates($currency->$methodName(), $isCustomSegment);
                    continue;
                }

                $value = $this->cleanValue($currency->$methodName());

                if($this->isAddable($value)) {
                    $isEmpty = false;
                    $currencyObject[Helper::getPropertyName($methodName)] = $value;
                }

            }

            $this->addToCurrencyDetails($currencyObject);

        }

        if($isEmpty) $this->ConstantDetails['currencies'] = [];

    }

    private function mapRates(array $rates, bool $isCustomSegment){

        if(!$isCustomSegment) return [];

        $ratesObject = [];

        /** @var RateObject $rate */
        foreach ($rates as $rate){

            $selling = $this->cleanValue($rate->getSelling());

            if(!$selling['value']) continue;

            $ratesObject[] = [
                'payment_type' => $rate->getPaymentMethodType(),
                'payment_method' => PaymentMethodType::PAYMENT_METHODS_ID[$rate->getPaymentMethodType()],
                'selling' => $rate->getSelling()
            ];

        }
        return $ratesObject;

    }


    /**
     * @param string $methodName
     * @param $value
     */
    private function addToConstantDetails(string $methodName, $value){
        $this->isAddable($value) ? $this->ConstantDetails[Helper::getPropertyName($methodName)] = $value : null;
    }

    /**
     * @param $value
     * @return bool
     */
    private function isAddable($value){

        return $value !== '' && $value !== null;
    }

    private function cleanValue($value){

        if(is_array($value)) {
            if(array_key_exists('value', $value)) $value['value'] = floatval(str_replace(',', '', $value['value']));
        }

        return $value;
    }

    /**
     * @param array $value
     */
    private function addToCurrencyDetails(array $value){
        $this->ConstantDetails['currencies'][] = $value;
    }

}