<?php

namespace App\Http\Controllers\Deed;

use App\Exceptions\AlertException;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Core\Auth;
use App\Models\Invite\QuestionTemplateModel;
use App\Models\User\InviteConfigModel;
use App\Models\User\LikeInviteQuestionModel;
use App\Models\User\UserModel;
use App\Services\Deed\InvitationService;
use Illuminate\Http\Request;

class InvitationController extends Controller
{

    /**
     * 发起心动邀请
     * @param  Request  $request
     * @return array
     * @throws AlertException
     */
    public function create(Request $request)
    {
        $uid = Auth::auth();

        $this->validate($request, [
            'invite_uid' => 'required|integer',
            'question_type' => 'in:0,1,2,3,4',
            'question_answer' => '',
            'question_id' => 'integer',
            'say_hello' => '',
            'kk' => '',
            'paint_id' => ''
        ]);
        $invite_uid = $request->post('invite_uid');
        $question_type = $request->post('question_type', 0);
        $question_id = $request->post('question_id', 0);
        $question_answer = $request->post('question_answer');
        $kk = $request->post('kk');
        $paint_id = $request->post('paint_id', 0);
        $request_content = $request->post('request_content');
        $question = array(
            'question_id' => $question_id,
            'question_answer' => $question_answer
        );
        $say_hello = $request->post("say_hello", null);
        $is = new InvitationService();
        $invite_id = $is->create(
            $uid,
            $invite_uid,
            $question_type,
            $question,
            $say_hello,
            $paint_id,
            $request_content,
            $kk
        );
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => [
                'invitate_id' => $invite_id
            ]
        ]);
    }

    /**
     * 通过id获取邀请信息
     * @param  int  $id
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function getbyid(int $id)
    {
        $uid = Auth::auth();
        $is = new InvitationService();
        $data = $is->getById($uid, $id);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 查看收到的待处理邀请列表
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function receivesByUnHandle(Request $request)
    {
        $uid = Auth::auth();
        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 100
        );

        $is = new InvitationService();
        $data = $is->receivesByUnhandle($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 查看待处理邀请列表
     * @param  Request  $request
     * @return array
     * @throws \Exception
     */
    public function unhandle(Request $request)
    {
        $uid = Auth::auth();
        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 100
        );

        $is = new InvitationService();
        $data = $is->unhandle($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 为心动考验支付小花
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * @throws AlertException
     */
    public function pay(Request $request)
    {
        $this->validate($request, ['invite_uid' => 'required', 'type' => 'required']);
        $type = $request->post('type', 1);
        $invite_uid = $request->post('invite_uid', 0);
        $uid = Auth::auth();

        $is = new InvitationService();
        $is->pay($uid, $invite_uid, $type);

        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 查看收到的邀请列表
     * @param  Request  $request
     * @return array
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function receives(Request $request)
    {
        $uid = Auth::auth();
        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 10
        );
        $onlyreceive = $request->get('onlyreceive') ?? false;

        $is = new InvitationService();
        $data = $is->receives($uid, $pages, $onlyreceive);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }


    /**
     * 查看发出的邀请列表
     * @param  Request  $request
     * @return array
     */
    public function sends(Request $request)
    {
        $uid = Auth::auth();
        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 20
        );
        $onlyreceive = $request->get('onlyreceive') ?? false;

        $is = new InvitationService();
        $data = $is->sends($uid, $pages, $onlyreceive);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }

    /**
     * 查看邀请列表
     * @param  Request  $request
     * @return array
     */
    public function list(Request $request)
    {
        $uid = Auth::auth();
        $pages = array(
            'page' => $request->get('page') ?? 1,
            'limit' => 20
        );

        $is = new InvitationService();
        $data = $is->list($uid, $pages);
        return array(
            'code' => 200,
            'message' => 'success',
            'data' => $data
        );
    }


    /**
     * 心动邀请同意
     * @param  int  $invite_id
     * @param  Request  $request
     * @return array
     * @throws AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function fixstate(int $invite_id, Request $request)
    {
        $this->validate($request, [
            'state' => 'required|in:-1,1',
        ]);
        $state = $request->post('state');
        $uid = Auth::auth();
        $is = new InvitationService();
        $is->fixstate($invite_id, $uid, $state);
        return [
            'code' => 200,
            'message' => 'success'
        ];
    }


    /**
     * 查阅心动邀请
     * @param  int  $invite_id
     * @return array
     * @throws AlertException
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
     */
    public function fixread(int $invite_id)
    {
        $uid = Auth::auth();
        $is = new InvitationService();
        $is->fixread($invite_id, $uid);
        return array(
            'code' => 200,
            'message' => 'success'
        );
    }


    /**
     * 获取某人设置的心动问题和
     * @param  Request  $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function questions(Request $request)
    {
        $type = $request->get('type', 'text');
        $uid = $request->get('uid');
        $sex = UserModel::where('uid', $uid)->value('sex') ?: 2;
        $config = InviteConfigModel::select('task_question_data', 'task_sing_data')->where('uid', $uid)->first();
        $template_ids = [];
        if ($config) {
            switch ($type) {
                case "text":
                    $template_ids = collect($config->task_question_data)->pluck('id')->toArray();
                    break;
                case 'music':
                    $template_ids = collect($config->task_sing_data)->pluck('id')->toArray();
                    break;
            }
        }
        if (!$template_ids) {
            $template_ids = array_random(QuestionTemplateModel::where(['type' => $type])->where(
                'select_count',
                '>',
                0
            )->pluck('id')->toArray(), 20);
        }
        $templates = QuestionTemplateModel::whereIn('id', $template_ids)
            ->where('type', $type)
            ->where('select_count', '>', 0)
            ->get();
        foreach ($templates as $template) {
            $voice = LikeInviteQuestionModel::where([
                'template_id' => $template->id,
                'select' => 1,
                'sex' => $sex
            ])->inRandomOrder()->first();
            unset($voice->question);
            $template->voice = $voice;
        }
        return response([
            'code' => 200,
            'message' => 'OK',
            'data' => $templates,
        ]);
    }
}
