<?php

namespace App\Http\Resources\ResidentialComplex;

use App\Repository\PlanningRepository;
use App\ResidentialComplex;
use App\RoomCount;
use App\RoomType;
use App\Section;
use Illuminate\Http\Resources\Json\JsonResource;

class ResidentialComplexFlatPrice extends JsonResource
{
    /** @var PlanningRepository */
    private $planningRepository;

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

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $result = [];
        /** @var ResidentialComplex $residentialComplex */
        $residentialComplex = $this;

        $residentialComplexFlatStatistic = $this->planningRepository->residentialComplex([
            'rc_slug' => $residentialComplex->code,
            'residential_complex_rooms_status' => true
        ]);
        $sectionFlatStatistic = $this->planningRepository->residentialComplex([
            'rc_slug' => $residentialComplex->code,
            'section_rooms_status' => true
        ]);

        $result['general_room_status'] = $this->getRCStatistic([
            'general_room_status' => true,
            'residential_complex_rooms_status' => $residentialComplexFlatStatistic
        ]);
        $result['section_rooms_status'] = $this->getRCStatistic([
            'section_rooms_status' => true,
            'section_rooms_status_array' => $sectionFlatStatistic
        ]);

        return $result;
    }

    private function getRCStatistic($parameters)
    {
        $statistic = [];
        if (isset($parameters['general_room_status'])) {
            $statistic = $parameters['residential_complex_rooms_status'];
        } elseif ($parameters['section_rooms_status']) {
            $statistic = $parameters['section_rooms_status_array'];
        }

        $result = [];
        /** @var Section $section */
        foreach ($statistic as $key => $section) {
            if (isset($parameters['general_room_status'])) {
            } elseif ($parameters['section_rooms_status']) {
                $result[$key] = [
                    'id' => $section->id,
                    'section_name' => $section->name ? $section->name : "",
                    'section_number' => $section->number ? $section->number : "",
                    'date' => $section->date_finish_buildiing ? $section->date_finish_buildiing : "",
                    'quarterly' => $section->quarterly_finish ? $section->quarterly_finish->code : "",
                ];
            }

            foreach (RoomCount::all() as $roomKey => $room) {
                $result[$key]['statistic'][$roomKey] = [
                    'room_count' => $room->count,
                    'room_type_id' => null,
                    'room_type' => null,
                    'min_space' => $section[$room->count . '_min_space'],
                    'max_space' => $section[$room->count . '_max_space'],
                    'min_price' => $section[$room->count . '_min_price'],
                    'max_price' => $section[$room->count . '_max_price'],
                    'min_cost_per_square_meter'
                    => $section[$room->count . '_min_cost_per_square_meter'],
                    'room_available' => $this->availableRoom($section, $room)
                ];
            }

            foreach (RoomType::all() as $roomTypeKey => $roomType) {
                $result[$key]['statistic'][$roomType->code] = [
                    'room_count' => "",
                    'room_type_id' => $roomType->id,
                    'room_type' => $roomType->code,
                    'min_space' => $section[$roomType->code . '_min_space'],
                    'max_space' => $section[$roomType->code . '_max_space'],
                    'min_price' => $section[$roomType->code . '_min_price'],
                    'max_price' => $section[$roomType->code . '_max_price'],
                    'min_cost_per_square_meter' =>
                        $section[$roomType->code . '_min_cost_per_square_meter'],
                    'room_available' => $this->availableRoom($section, $roomType)
                ];
            }
        }

        return $result;
    }

    /**
     * @param $section
     * @param $type
     * @return string
     */
    public function availableRoom($section, $type)
    {
        if ($type instanceof RoomCount) {
            $roomCount = $section[$type->count . '_amount_of_apartment'];
            $available = $section[$type->count . '_available'];
            $sold = $section[$type->count . '_sold'];
        } elseif ($type instanceof RoomType) {
            $roomCount = $section[$type->code . '_amount_of_apartment'];
            $available = $section[$type->code . '_available'];
            $sold = $section[$type->code . '_sold'];
        }

        if ($available > 0 || $sold > 0) {
            if ($sold > 0 && $available == 0) {
                $roomCount = 'sold';
            } elseif ($sold == 0 && $available > 0) {
                if ($roomCount == 0) {
                    $roomCount = 'information_not_available';
                }
            }
        } else {
            $roomCount = 'information_not_available';
        }

        return  $roomCount;
    }
}
