<?php

namespace App\Http\Controllers;

use App\Http\Api\ChannelApi;
use App\Http\Api\MdRender;
use App\Models\DhammaTerm;
use App\Models\NissayaEnding;
use App\Models\Relation;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;

class NissayaCardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @return Response
     */
    public function show(Request $request, string $nissayaEnding)
    {
        //
        $cardData = [];
        App::setLocale($request->input('lang'));
        $localTerm = ChannelApi::getSysChannel(
            '_System_Grammar_Term_'.strtolower($request->input('lang')).'_',
            '_System_Grammar_Term_en_'
        );

        if (! $localTerm) {
            return $this->error('no term channel');
        }

        $cardData['ending']['word'] = $nissayaEnding;
        $endingTerm = DhammaTerm::where('channal', $localTerm)
            ->where('word', $nissayaEnding)
            ->first();
        if ($endingTerm) {
            $cardData['ending']['id'] = $endingTerm->guid;
            $cardData['ending']['tag'] = $endingTerm->tag;
            $cardData['ending']['meaning'] = $endingTerm->meaning;
            $cardData['ending']['note'] = $endingTerm->note;
            if (! empty($endingTerm->note)) {
                $mdRender = new MdRender(
                    [
                        'mode' => 'read',
                        'format' => 'react',
                        'lang' => $endingTerm->lang,
                    ]
                );
                $cardData['ending']['html'] = $mdRender->convert($endingTerm->note, [], null);
            }
        }

        $myEnding = NissayaEnding::where('ending', $nissayaEnding)
            ->select('relation')->get();
        if (count($myEnding) === 0) {
            if (! isset($cardData['ending']['note'])) {
                $cardData['ending']['note'] = "no record\n";
            }
        }

        $relations = Relation::whereIn('name', $myEnding)->get();

        if (count($relations) > 0) {
            $cardData['title_case'] = '本词';
            $cardData['title_relation'] = '关系';
            $cardData['title_local_relation'] = '关系';
            $cardData['title_local_link_to'] = '目标词特征';
            $cardData['title_content'] = '含义';
            $cardData['title_local_ending'] = '翻译建议';

            foreach ($relations as $key => $relation) {
                $newLine = [];
                $relationInTerm = DhammaTerm::where('channal', $localTerm)
                    ->where('word', $relation['name'])
                    ->first();
                if (empty($relation->from)) {
                    $cardData['row'][] = ['relation' => $relation->name];

                    continue;
                }
                $from = json_decode($relation->from);
                if (isset($from->case)) {
                    $cases = $from->case;
                    $localCase = [];
                    foreach ($cases as $case) {
                        $localCase[] = [
                            'label' => DhammaTerm::where('channal', $localTerm)
                                ->where('word', $case)
                                ->value('meaning'),
                            'case' => $case,
                            'link' => config('mint.server.dashboard_base_path').'/term/list/'.$case,
                        ];
                    }
                    // 格位
                    $newLine['from']['case'] = $localCase;
                }
                if (isset($from->spell)) {
                    $newLine['from']['spell'] = $from->spell;
                }
                // 连接到
                $linkTos = json_decode($relation->to);
                if (isset($linkTos->case) && is_array($linkTos->case) && count($linkTos->case) > 0) {
                    $localTo = [];
                    foreach ($linkTos->case as $to) {
                        $localTo[] = [
                            'label' => DhammaTerm::where('channal', $localTerm)->where('word', $to)->value('meaning'),
                            'case' => $to,
                            'link' => config('mint.server.dashboard_base_path').'/term/list/'.$to,
                        ];
                    }

                    // 格位
                    $newLine['to']['case'] = $localTo;
                }
                if (isset($linkTos->spell)) {
                    $newLine['to']['spell'] = $linkTos->spell;
                }
                // 含义 用分类字段的term 数据
                if (isset($relation['category']) && ! empty($relation['category'])) {
                    $newLine['category']['name'] = $relation['category'];
                    $localCategory = DhammaTerm::where('channal', $localTerm)
                        ->where('word', $relation['category'])
                        ->first();

                    if ($localCategory) {
                        $mdRender = new MdRender(
                            [
                                'mode' => 'read',
                                'format' => 'text',
                                'lang' => $endingTerm->lang,
                            ]
                        );
                        $newLine['category']['note'] = $mdRender->convert($localCategory->note, [], null);
                        $newLine['category']['meaning'] = $localCategory->meaning;
                    } else {
                        $newLine['category']['note'] = $relation['category'];
                        $newLine['category']['meaning'] = $relation['category'];
                    }
                }

                /**
                 * 翻译建议
                 * relation 和 from 都匹配成功
                 * from 为空 只匹配 relation
                 */
                $arrLocalEnding = [];
                $localEndings = NissayaEnding::where('relation', $relation['name'])
                    ->where('lang', $request->input('lang'))
                    ->get();
                foreach ($localEndings as $localEnding) {
                    if (empty($localEnding->from) || $localEnding->from === $relation->from) {
                        $arrLocalEnding[] = $localEnding->ending;
                    }
                }
                $newLine['local_ending'] = implode(';', $arrLocalEnding);

                // 本地语言 关系名称
                if ($relationInTerm) {
                    $newLine['local_relation'] = $relationInTerm->meaning;
                }
                // 关系名称
                $newLine['relation'] = $relation['name'];
                $newLine['relation_link'] = config('mint.server.dashboard_base_path').'/term/list/'.$relation['name'];
                $cardData['row'][] = $newLine;
            }
        }

        if ($request->input('content_type', 'markdown') === 'markdown') {
            $m = new \Mustache_Engine(['entity_flags' => ENT_QUOTES]);
            $tpl = file_get_contents(resource_path('mustache/nissaya_ending_card.tpl'));
            $result = $m->render($tpl, $cardData);
        } else {
            $result = $cardData;
        }

        return $this->ok($result);
    }

    /**
     * Update the specified resource in storage.
     *
     * @return Response
     */
    public function update(Request $request, NissayaEnding $nissayaEnding)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @return Response
     */
    public function destroy(NissayaEnding $nissayaEnding)
    {
        //
    }
}
