<?php

namespace App\Http\Resources;

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

class TermOfPaymentCollection 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)
    {
        $result = [];
        /**
         * @var  $key
         * @var TermsOfPayment $termsOfPayment
         */
        foreach ($this->collection as $key => $termsOfPayment) {
            $termsOfPaymentArray = [];
            $termsOfPaymentArray['header'] = $termsOfPayment->header;
            $termsOfPaymentArray['color'] = $termsOfPayment->color_of_elements;
            $termsOfPaymentArray['can_send_message_to_developer'] = $termsOfPayment->appeal;

            if (!empty($termsOfPayment->content_option_one)) {
                $termsOfPaymentArray['content'][] = $termsOfPayment->content_option_one;
            }

            if (!empty($termsOfPayment->content_option_two)) {
                $termsOfPaymentArray['content'][] = $termsOfPayment->content_option_two;
            }

            if (!empty($termsOfPayment->content_option_three)) {
                $termsOfPaymentArray['content'][] = $termsOfPayment->content_option_three;
            }

            $result[] = $termsOfPaymentArray;
        }

        return $result;
    }
}
