<?php

namespace App\Http\Controllers\Experience;

use App\Models\Wechat\FormIdModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TuodanController extends Controller
{
    //
    public function index(Request $request)
    {
        $openid = $request->openid;
        $options = $request->options;

        $score = $this->option($options);

        $data = $this->result($score);

        \DB::table('kdgx_mini_tuodan')->insert([
            'openid' => $openid,
            'created_at' => time(),
            'updated_at' => time(),
            'data' => json_encode($data)
        ]);
        return [
            'code' => 200,
            'message' => 'OK',
            'data' => $data
        ];
    }


    /**
     * 计算选项得分
     */
    private function option(array $options)
    {
        $score = 0;
        foreach ($options as $key => $option) {
            switch ($key) {
                case '1':
                    switch ($option) {
                        case '0':
                        case '1':
                            break;
                    }
                    break;
                case '2':
                    switch ($option) {
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                            break;
                    }
                    break;
                case '3':
                    switch ($option) {
                        case '0':
                            $score += 30;
                            break;
                        case '1':
                            $score += 10;
                            break;
                    }
                    break;
                case '4':
                    switch ($option) {
                        case '0':
                            $score += 10;
                            break;
                        case '1':
                            $score += 30;
                            break;
                    }
                    break;
                case '5':
                    switch ($option) {
                        case '0':
                            break;
                        case '1':
                            $score += 30;
                            break;
                    }
                    break;
                case '6':
                    switch ($option) {
                        case '0':
                        case '1':
                            break;
                    }
                    break;
            }
        }
        return $score;
    }


    public function result(int $score)
    {

        switch ($score) {
            case '20':
                $type = rand(2, 4);
                $day = 0;
                break;
            case '40':
            case '30':
                $type = 1;
                $day = rand(361, 1800);
                break;
            case '60':
            case '70':
                $type = 1;
                $day = rand(31, 360);
                break;
            case '90':
                $type = 1;
                $day = rand(1, 30);
                break;
            default:
                $type = rand(2, 4);
                $day = 0;
        }
        $list = array("游戏里", "聚会", "婚礼", "健身房", "美食街", "学校", "旅行中", "商场", "咖啡厅", "公园", "公交车", "地铁里");
        $key = array_rand($list);
        $place = $list[$key];
        return [
            'type' => $type,
            'day' => $day,
            'place' => $place
        ];
    }


    /**
     * 几率formId
     * @param  Request  $request
     * @return array
     */
    public function form(Request $request)
    {
        $this->validate($request, [
            'form_id' => 'required',
            'openid' => 'required',
            'public_id' => 'required',
        ]);
        FormIdModel::create($request->all());
        return [
            'code' => 200,
            'message' => 'OK'
        ];
    }


    public function info()
    {
        return [
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'is_qrcode' => 0,
            ]
        ];
    }
}
