<?php

namespace App\Http\Resources;

use App\Http\Api\ChannelApi;
use App\Http\Api\MdRender;
use App\Http\Api\StudioApi;
use App\Http\Api\SuggestionApi;
use App\Http\Api\UserApi;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class SentResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  Request  $request
     * @return array|Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $channel = ChannelApi::getById($this->channel_uid);
        if (! $channel) {
            Log::error('channel left', ['data' => $this->channel_uid, 'uid' => $this->uid]);
        }
        if ($request->input('mode', 'read') === 'read') {
            $mode = 'read';
        } else {
            $mode = 'edit';
        }
        $data = [
            'id' => $this->uid,
            'content' => $this->content,
            'content_type' => $this->content_type,
            'html' => '',
            'book' => $this->book_id,
            'paragraph' => $this->paragraph,
            'word_start' => $this->word_start,
            'word_end' => $this->word_end,
            'editor' => UserApi::getByUuid($this->editor_uid),
            'fork_at' => $this->fork_at,
            'updated_at' => $this->updated_at,
        ];

        if ($channel) {
            $data['channel'] = $channel;
            $data['studio'] = StudioApi::getById($channel['studio_id']);
        }
        if ($request->has('channels')) {
            $channels = explode(',', $request->input('channels'));
        } else {
            $channels = [$this->channel_uid];
        }
        // TODO 找出channel id = '' 的原因
        $mChannels = [];
        foreach ($channels as $key => $value) {
            if (Str::isUuid($value)) {
                $mChannels[] = $value;
            }
        }
        if ($request->input('html', true)) {
            $data['html'] = MdRender::render(
                $this->content,
                $mChannels,
                null,
                $mode,
                $channel['type'],
                $this->content_type,
                $request->input('format', 'react')
            );
        }
        if ($request->input('mode') === 'edit' || $request->input('mode') === 'wbw') {
            $data['suggestionCount'] = SuggestionApi::getCountBySent(
                $this->book_id,
                $this->paragraph,
                $this->word_start,
                $this->word_end,
                $this->channel_uid
            );
        }
        if (isset($this->acceptor_uid) && ! empty($this->acceptor_uid)) {
            $data['acceptor'] = UserApi::getByUuid($this->acceptor_uid);
            $data['pr_edit_at'] = $this->pr_edit_at;
        }

        return $data;
    }
}
