<?php

namespace App\Http\Resources;

use App\Http\Controllers\Traits\PriceFormatTrait;
use Illuminate\Http\Resources\Json\ResourceCollection;

class SectionPriceCollection extends ResourceCollection
{
    use PriceFormatTrait;

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

    /**
     * Create a new resource instance.
     *
     * @param  mixed $resource
     * @param array $parameters
     */
    public function __construct($resource, $parameters = null)
    {
        parent::__construct($resource);
        $this->resource = $resource;
        $this->parameters = is_array($parameters) ? $parameters : [];
    }

    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $this->getExchangeRate();

        $isFormat = isset($this->parameters['not_format_price']) ? false : true;
        $result = [];
        $result['general'][0]['statistic'] = [];
        $result['sections'] = [];
        $idSections = [];
        foreach ($this->collection as $key => $price) {
            $section = $price->section;
            if ($section) {
                if (!in_array($section->id, $idSections)) {
                    $idSections[] = $section->id;
                }
                $sectionName = $section->name ? $section->name : "";

            } else {
                $sectionName = 'general';
            }

            if ($sectionName == 'general') {
                $groupKey = 'general';
                $result[$groupKey][0]['statistic'][] = [
                    'room_count' => $price->room_count_id,
                    'room_type' => $price->room_type ? $price->room_type->code : '',
                    'min_price' => (int)$price->min_price * $this->exchangeRate,
                    'max_price' => (int)$price->max_price * $this->exchangeRate,
                    'price_per_squera_meter' => (int)$price->price_per_squera_meter * $this->exchangeRate,
                    'price_per_squera_meter_max' => (int)$price->price_per_squera_meter_max * $this->exchangeRate,
                    'min_space' => $price->min_space,
                    'max_space' => $price->max_space,
                    'number_of_apartments' => $price->number_of_apartments,
                ];
            } else {
                $groupKey = 'sections';
                $sectionKey = array_keys($idSections, $section->id);
                if (!isset($result[$groupKey][$sectionKey[0]])) {
                    $result[$groupKey][$sectionKey[0]] = [
                        'id' => $section->id,
                        'section_name' => $section->name ? $section->name : "",
                        'date' => $section->date_finish_buildiing ? $section->date_finish_buildiing : "",
                        'quarterly' => $section->quarterly_finish ? $section->quarterly_finish->code : "",
                    ];
                }

                $result[$groupKey][$sectionKey[0]]['statistic'][] = [
                    'room_count' => $price->room_count_id,
                    'room_type' => $price->room_type ? $price->room_type->code : '',
                    'min_price' => (int)$price->min_price * $this->exchangeRate ,
                    'max_price' => (int)$price->max_price * $this->exchangeRate ,
                    'price_per_squera_meter' => (int)$price->price_per_squera_meter * $this->exchangeRate ,
                    'price_per_squera_meter_max' => (int)$price->price_per_squera_meter_max * $this->exchangeRate ,
                    'min_space' => $price->min_space,
                    'max_space' => $price->max_space,
//                    'min_price' => $this->formatPrice($price->min_price, $isFormat),
//                    'max_price' => $this->formatPrice($price->max_price, $isFormat),
//                    'price_per_squera_meter' => $this->formatPrice($price->price_per_squera_meter, $isFormat),
//                    'price_per_squera_meter_max' => $this->formatPrice($price->price_per_squera_meter_max, $isFormat),
                    'number_of_apartments' => $price->number_of_apartments,
                ];
            }
        }

        if (isset($result['sections'])) {
            $regroupSectionResult = collect($result['sections']);
            $result['sections'] = $regroupSectionResult->sortBy('section_name')->toArray();
        }

        return $result;
    }
}
