<?php

namespace App\Http\Controllers\Syj;

use App\Models\Syj\GameLocationModel;
use App\Models\Syj\TacitPaintModel;
use App\Models\Syj\TacitQuestionAnswerModel;
use App\Models\Syj\TacitQuestionModel;
use App\Services\Vendor\Geohash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GameController extends Controller
{


    /**
     * 玩法一
     *
     *
     */


    /**
     * 画左边
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function drawLeft(Request $request)
    {
        $this->validate($request, [
            'photo' => 'required',
            'uuid' => 'required',
        ]);
        $photo = TacitPaintModel::create([
            'openid' => $request->header('openid', ''),
            'photo' => $request->photo,
            'uuid' => $request->uuid,
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
        ]);
    }


    /**
     * 画右边
     * @param $uuid
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function drawRight($uuid, Request $request)
    {
        $this->validate($request, [
            'photo' => 'required',
            'uuid' => 'required',
        ]);
        $photo = TacitPaintModel::where('uuid', $uuid)->firstOrFail();
        TacitPaintModel::create([
            'openid' => $request->header('openid', ''),
            'photo' => $request->photo,
            'uuid' => $request->uuid,
            'parent_id' => $photo->id,
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
        ]);
    }


    /**
     * 查看照片
     * @param $uuid
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getPhoto($uuid)
    {
        $photo = TacitPaintModel::where('uuid', $uuid)->firstOrFail();
        $photo->user;
        if ($photo->parent_id) {
            $parent = TacitPaintModel::find($photo->parent_id);
            $parent->user;
            $photo->parent = $parent;
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $photo
        ]);
    }







    /*
     * 玩法二
     *
     *
     */


    /**
     * 创建首次问题
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function createAnswers(Request $request)
    {
        $this->validate($request, [
            'questions' => 'required',
            'answers' => 'required',
            'uuid' => 'required',
        ]);
        $question = TacitQuestionAnswerModel::create([
            'openid' => $request->header('openid'),
            'questions' => $request->questions,
            'answers' => $request->answers,
            'uuid' => $request->uuid,
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
        ]);
    }


    /**
     * 对面测试问题
     * @param  int  $question_id
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function updateAnswers($uuid, Request $request)
    {
        $this->validate($request, [
            'answers' => 'required',
            'uuid' => 'required',
        ]);
        $answer = TacitQuestionAnswerModel::where('uuid', $uuid)->firstOrFail();
        TacitQuestionAnswerModel::create([
            'openid' => $request->header('openid', ''),
            'questions' => $answer->questions,
            'answers' => $request->answers,
            'parent_id' => $answer->id,
            'uuid' => $request->uuid,
        ]);

        $count = $this->compare($answer->answers, $request->answers);
        $tacit_degree = is_int($count) ? round(100 * $count / count($answer->questions)) : rand(0, 100);
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'tacit_degree' => $tacit_degree
            ]
        ]);
    }


    /**
     * 获取随机一体
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getRandomQuestion(Request $request)
    {
        $question = TacitQuestionModel::when($request->get('filter'), function ($query) use ($request) {
            $filter = explode(',', $request->get('filter'));
            return $query->whereNotIn('id', $filter);
        })->inRandomOrder()->first();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $question
        ]);
    }

    /**
     * 获取问题
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getQuestion(Request $request)
    {
        $questions = TacitQuestionModel::select('id', 'question', 'options')
            ->when($request->filled('uuid'), function ($query) use ($request) {
                $answer = TacitQuestionAnswerModel::where('uuid', $request->uuid)->firstOrFail();
                return $query->whereIn('id', $answer->questions);
            })->inRandomOrder()
            ->limit(10)
            ->get();
        $questions = $questions->sortBy(function ($question) {
            return $question->id;
        })->values()->all();
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $questions
        ]);
    }


    /**
     * 获取问题
     * @param  int  $question_id
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getAnswers($uuid, Request $request)
    {
        $answer = TacitQuestionAnswerModel::where('uuid', $uuid)->firstOrFail();
        $answer->user;
        if ($answer->parent_id) {
            $parent = TacitQuestionAnswerModel::find($answer->parent_id);
            $parent->user;
            $answer->parent = $parent;
            $count = $this->compare($answer->answers, $parent->answers);
            $answer->tacit_degree = is_int($count) ? round(100 * $count / count($answer->questions)) : rand(0, 100);
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $answer
        ]);
    }


    /**
     * 对比匹配率
     * @param $array1
     * @param $array2
     * @return bool|int
     */
    private function compare($array1, $array2)
    {
        if (count($array1, 1) != count($array2, 1)) {
            return false;
        } elseif (count($array1) != count($array2)) {
            return false;
        } elseif (count($array1) != count($array1, 1)) {
            return false;
        }
        $count = 0;
        foreach ($array1 as $key => $value) {
            if ($value == $array2[$key]) {
                $count++;
            }
        }
        return $count;
    }





    /**
     * 玩法三
     *
     *
     */


    /**
     * 前者创建定位
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function createLocation(Request $request)
    {
        $this->validate($request, [
            'uuid' => 'required',
            'lng' => 'required',
            'lat' => 'required',
        ]);
        $location = GameLocationModel::create([
            'openid' => $request->header('openid'),
            'uuid' => $request->uuid,
            'lng' => $request->lng,
            'lat' => $request->lat,
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $location
        ]);
    }


    /**
     * 后者定位
     * @param  Request  $request
     * @param $uuid
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function updateLocation(Request $request, $uuid)
    {
        $this->validate($request, [
            'uuid' => 'required',
            'lng' => 'required',
            'lat' => 'required',
        ]);
        $parent = GameLocationModel::where('uuid', $uuid)->firstOrFail();
        $location = GameLocationModel::create([
            'openid' => $request->header('openid'),
            'uuid' => $request->uuid,
            'lng' => $request->lng,
            'lat' => $request->lat,
            'parent_id' => $parent->id
        ]);
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $location
        ]);
    }


    /**
     * 获取位置信息
     * @param $uuid
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function getLocation($uuid)
    {
        $location = GameLocationModel::where('uuid', $uuid)->firstOrFail();
        $location->user;
        if ($location->parent_id) {
            $parent = GameLocationModel::find($location->parent_id);
            $parent->user;
            $location->parent = $parent;
            $geo = new Geohash();
            $location->distance = sprintf(
                '%.1f',
                $geo->getDistance($location->lat, $location->lng, $parent->lat, $parent->lng) / 1000
            );
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $location
        ]);
    }
}
