<?php

namespace App\Home\Controllers;

use Storage;
use App\Http\Controllers\Controller;
use App\Model\Question;
use App\Model\Option;
use App\Jobs\Robot;

class IndexController extends Controller
{
	public function importTimu()
	{
        set_time_limit(0);
        $txt = Storage::get('1.txt');
        $txt_arr = explode("\n", $txt);
        foreach ($txt_arr as $k => $val) {
            $q_a = explode('？', $val);
            $str1 = preg_replace('/^\d/i','',$q_a[0], 1);
            $str2 = preg_replace('/^\d/i','',$str1, 1);
            $title =  preg_replace('/^\d/i','',$str2, 1);
            $question_id = Question::insertGetId([
                "title" => $title . "?",
                "is_released" => 1,
                "created_at" => date('Y-m-d H:i:s'),
                "updated_at" => date('Y-m-d H:i:s'),
            ]);
            $optiion = mb_substr($q_a[1], 0, -4);

            //答案
            $answer = mb_substr($q_a[1], -4);
            $answer = mb_substr($answer, 1,1);

            // echo $optiion;
            // echo mb_strpos($optiion, 'B');
            $o_answer = 0;
            if(mb_strpos($optiion, 'A')){
                $optiion_A = mb_substr($optiion, mb_strpos($optiion, 'A') + 1, mb_strpos($optiion, 'B') - mb_strpos($optiion, 'A') - 1);
            }else{
                $optiion_A = mb_substr($optiion, 0 ,mb_strpos($optiion, 'B') - 1);
            }
            if($answer == 'A'){
                $o_answer = 1;
            }
            Option::insert([
                "title" => $optiion_A,
                "is_answer" => $o_answer,
"question_id" => $question_id,
                "created_at" => date('Y-m-d H:i:s'),
                "updated_at" => date('Y-m-d H:i:s'),
            ]);
            $o_answer = 0;

            if(mb_strpos($optiion, 'C')){

                $option_B = mb_substr($optiion, mb_strpos($optiion, 'B') + 1, mb_strpos($optiion, 'C') - mb_strpos($optiion, 'B') - 1);
                if($answer == 'B'){
                    $o_answer = 1;
                }
                Option::insert([
                    "title" => $option_B,
                    "is_answer" => $o_answer,
"question_id" => $question_id,
                    "created_at" => date('Y-m-d H:i:s'),
                    "updated_at" => date('Y-m-d H:i:s'),
                ]);
                $o_answer = 0;
            }elseif(mb_strpos($optiion, 'B')){
                echo $option_B = mb_substr($optiion, mb_strpos($optiion, 'B') + 1);
                if($answer == 'B'){
                    $o_answer = 1;
                }
                Option::insert([
                    "title" => $option_B,
                    "is_answer" => $o_answer,
"question_id" => $question_id,
                    "created_at" => date('Y-m-d H:i:s'),
                    "updated_at" => date('Y-m-d H:i:s'),
                ]);
                $o_answer = 0;                
            }
            if(mb_strpos($optiion, 'D')){
                $option_C = mb_substr($optiion, mb_strpos($optiion, 'C') + 1, mb_strpos($optiion, 'D') - mb_strpos($optiion, 'C') - 1);
                if($answer == 'C'){
                    $o_answer = 1;
                }
                Option::insert([
                    "title" => $option_C,
                    "is_answer" => $o_answer,
"question_id" => $question_id,
                    "created_at" => date('Y-m-d H:i:s'),
                    "updated_at" => date('Y-m-d H:i:s'),
                ]);
                $o_answer = 0;
            }elseif(mb_strpos($optiion, 'C')){
                echo $option_C = mb_substr($optiion, mb_strpos($optiion, 'C') + 1);
                if($answer == 'C'){
                    $o_answer = 1;
                }
                Option::insert([
                    "title" => $option_C,
                    "is_answer" => $o_answer,
"question_id" => $question_id,
                    "created_at" => date('Y-m-d H:i:s'),
                    "updated_at" => date('Y-m-d H:i:s'),
                ]);
                $o_answer = 0;
            }
            if(mb_strpos($optiion, 'E')){
                $option_D = mb_substr($optiion, mb_strpos($optiion, 'D') + 1, mb_strpos($optiion, 'E') - mb_strpos($optiion, 'D') - 1);
                if($answer == 'B'){
                    $o_answer = 1;
                }
                Option::insert([
                    "title" => $option_D,
                    "is_answer" => $o_answer,
"question_id" => $question_id,
                    "created_at" => date('Y-m-d H:i:s'),
                    "updated_at" => date('Y-m-d H:i:s'),
                ]);
                $o_answer = 0;

            }elseif(mb_strpos($optiion, 'D')){
                $option_D = mb_substr($optiion, mb_strpos($optiion, 'D') + 1);
                if($answer == 'D'){
                    $o_answer = 1;
                }
                Option::insert([
                    "title" => $option_D,
                    "is_answer" => $o_answer,
"question_id" => $question_id,
                    "created_at" => date('Y-m-d H:i:s'),
                    "updated_at" => date('Y-m-d H:i:s'),
                ]);
                $o_answer = 0;
            }
        }
	}

    /**
     * 测试加入队列
     */
    public function putJob()
    {
        dump(Robot::dispatch()->onQueue('robot'));
    }

}