<?php

namespace App\Http\Controllers\Game;

use App\Models\Game\CycAnserModel;
use App\Models\Game\CycLogModel;
use App\Models\Game\CycQuestionModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CycController extends Controller
{


    // 列表
    public function index(Request $request)
    {
        $uid = 0;
        $openid = $request->header('openid');
        $questions = CycQuestionModel::where([['show_at', '<>', 0], ['show_at', '<=', time()]])->get();
        foreach ($questions as $question) {
            if (
                CycLogModel::when($uid, function ($query) use ($uid) {
                    return $query->where('uid', $uid);
                }, function ($query) use ($openid) {
                    return $query->where('openid', $openid);
                })->where('question_id', $question->id)
                ->first()
            ) {
                $question->is_complete = 1;
            } else {
                $question->is_complete = 0;
            }
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $questions
        ]);
    }


    /**
     * @param  Request  $request
     * @param $question_id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function show(Request $request, $question_id)
    {
        $question = CycQuestionModel::find($question_id);
        $uid = 0;
        $openid = $request->header('openid');

        if (
            CycLogModel::when($uid, function ($query) use ($uid) {
                return $query->where('uid', $uid);
            }, function ($query) use ($openid) {
                return $query->where('openid', $openid);
            })->where(['question_id' => $question->id])->first()
        ) {
            $question->is_complete = 1;
        } else {
            $question->is_complete = 0;
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $question
        ]);
    }


    /**
     * 回答
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function answer(Request $request, $question_id)
    {
        $this->validate($request, [
            'answer' => 'required'
        ]);
        $uid = 0;
        $openid = $request->header('openid', '');
        $answer_option = $request->input('answer');
        $question = CycQuestionModel::find($question_id);
        foreach ($question->answers as $value) {
            if (in_array($answer_option, $value['answers'])) {
                $answer_id = $value['id'];
                break;
            }
        }
        $answer = CycAnserModel::find($answer_id);
        $log = CycLogModel::create([
            'uid' => $uid,
            'openid' => $openid,
            'question_id' => $question_id,
            'answer_option' => $answer_option,
            'answer' => $answer_id
        ]);
        $log->answer = $answer;
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $log
        ]);
    }
}
