<?php

namespace App\Http\Controllers\Deed;

use App\Http\Resources\Invite\QuestionCollection;
use App\Http\Resources\Invite\QuestionResource;
use App\Http\Resources\Invite\QuestionTagCollection;
use App\Models\Invite\QuestionTemplateModel;
use App\Models\Invite\QuestionTagModel;
use App\Models\User\InviteConfigModel;
use App\Models\User\LikeInviteQuestionModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class QuestionController extends Controller
{
    // 问题列表
    public function index(Request $request)
    {
        $type = $request->get('type', 'text');
        $questions = QuestionTemplateModel::where('type', $type)
            ->display(true)
            ->get();
        foreach ($questions as $question) {
            $question->tag;
            switch ($question->type) {
                case "music":
                    $question->question = json_decode($question->question, true);
                    break;
            }
        }
        return new QuestionCollection($questions);
    }


    /**
     * 获取指定问题
     * @param $id
     * @return QuestionResource
     */
    public function show($id)
    {
        $question = QuestionTemplateModel::findOrFail($id);
        $question->tag;
        switch ($question->type) {
            case "music":
                $question->question = json_decode($question->question, true);
                break;
        }
        return new QuestionResource($question);
    }


    /**
     * 获取随机一个问题
     * @param  Request  $request
     * @param $id
     * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function random(Request $request, $id)
    {
        $type = $request->get('type', 'text');
        $question = QuestionTemplateModel::where('type', $type)
            ->display(true)
            ->where('id', '<>', $id)
            ->inRandomOrder()
            ->firstOrFail(['id', 'type', 'question', 'tag_id', 'author']);
        $question->tag;
        switch ($question->type) {
            case "music":
                $question->question = json_decode($question->question, true);
                break;
        }
        return new QuestionResource($question);
    }


    /**
     * 获取随机一个问题
     * @param  Request  $request
     * @param $id
     * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function randomBatch(Request $request)
    {
        $type = $request->get('type', 'text');
        $question_ids = QuestionTemplateModel::where('type', $type)
            ->display(true)
            ->when($request->filled('filter'), function ($query) use ($request) {
                return $query->whereNotIn('id', explode(',', $request->get('filter')));
            })->pluck('id')->toArray();

        $random_ids = array_random($question_ids, 10);

        $questions = QuestionTemplateModel::find($random_ids);
        foreach ($questions as $question) {
            switch ($question->type) {
                case 'music':
                    $question->question = json_decode($question->question, true);
                    break;
            }
        }
        return new QuestionCollection($questions);
    }


    /**
     * 获取随机一个问题
     * @param  Request  $request
     * @return QuestionTemplateModel|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function filter(Request $request)
    {
        $type = $request->get('type', 'text');
        $question = QuestionTemplateModel::where('type', $type)
            ->display(true)
            ->when($request->filled('filter'), function ($query) use ($request) {
                return $query->whereNotIn('id', explode(',', $request->get('filter')));
            })->inRandomOrder()
            ->firstOrFail(['id', 'type', 'question', 'tag_id', 'author']);
        $question->tag;
        switch ($question->type) {
            case "music":
                $question->question = json_decode($question->question, true);
                break;
        }
        return new QuestionResource($question);
    }

    /**
     * 标签列表
     * @param  Request  $request
     * @return QuestionTagCollection
     */
    public function tags(Request $request)
    {
        $type = $request->get('type', 'text');
        $tags = QuestionTagModel::where('type', $type)->display(true)->get();
        return new QuestionTagCollection($tags);
    }


    /**
     * 标签下的问题
     * @param  Request  $request
     * @param $id
     * @return QuestionCollection
     */
    public function tagQuestions(Request $request, $id)
    {
        $tag = QuestionTagModel::findOrFail($id);
        $questions = QuestionTemplateModel::where('tag_id', $tag->id)
            ->display(true)
            ->when($request->get('leader'), function ($query) use ($request) {
                return $query->where('select_count', '>', 0);
            })
            ->paginate($request->input('pre_page', 20));
        foreach ($questions as $question) {
            $question->tag;
            switch ($question->type) {
                case "music":
                case "song":
                    $question->question = json_decode($question->question, true);
                    break;
            }
        }
        return new QuestionCollection($questions);
    }


    /**
     * 获取领唱信息
     * @param  Request  $request
     * @param $template_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function showSingerByTemplate(Request $request, $template_id)
    {

        $signer = LikeInviteQuestionModel::where('question_type', 1)
            ->where('select', 1)
            ->where('template_id', $template_id)
            ->inRandomOrder()
            ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'author', 'voice_src', 'template_id']);
        $signer->user;
        $signer->tag;
        $question = QuestionTemplateModel::find($signer->template_id);
        $question->question = json_decode($question->question, true);
        $signer->question = $question;
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $signer
        ]);
    }


    /**
     * 获取随机领唱
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function randomLeadSinger(Request $request)
    {
        if (
            $request->filled('uid')
            & $like_questions = InviteConfigModel::where('uid', $request->input('uid'))->value('task_sing_data')
        ) {
            // 用户设置的问题中过滤filter后，随机选择一体题
            $template_id = array_random(array_diff(
                array_column($like_questions, 'id'),
                explode(',', $request->get('filter'))
            ));
            $signer = LikeInviteQuestionModel::where('question_type', 1)
                ->where('select', 1)
                ->where('template_id', $template_id)
                ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
        } else {
            $signer = LikeInviteQuestionModel::where('question_type', 1)
                ->where('select', 1)
                ->when($request->get('filter'), function ($query) use ($request) {
                    return $query->whereNotIn('template_id', explode(',', $request->get('filter')));
                })->inRandomOrder()
                ->firstOrFail(['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
        }

        $signer->user;
        $signer->tag;
        $question = QuestionTemplateModel::findOrFail($signer->template_id);
        /** @var QuestionTemplateModel $question */
        $question->question = json_decode($question->question, true);
        $signer->question = $question;
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $signer
        ]);
    }


    /**
     * 获取指定领唱
     * @param  Request  $request
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getLeadSinger(Request $request, $id)
    {
        $signer = LikeInviteQuestionModel::where('question_type', 1)
            ->where('select', 1)
            ->findOrFail($id, ['id', 'uid', 'question', 'tag_id', 'voice_src', 'template_id']);
        $signer->user;
        $signer->tag;
        $question = QuestionTemplateModel::find($signer->template_id);
        $question->question = json_decode($question->question, true);
        $signer->question = $question;
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $signer
        ]);
    }
}
