<?php

namespace App\Services;

use App\Models\PcCao;
use App\Models\PcCaoHistory;
use App\Models\PcIssue;
use App\Models\PcPrediction;

class PcIssueService extends BaseService
{
    public static $MODEL = PcIssue::class;

    private static function getKeno(): array
    {
        $numbers = range(1, 80);
        shuffle($numbers);
        $random_numbers = array_slice($numbers, 0, 20);
        sort($random_numbers);
        return $random_numbers;
    }

    private static function getWinningNumbers($keno): array
    {
        $winningNumbers = [];
        $sum = $keno[1] + $keno[4] + $keno[7] + $keno[10] + $keno[13] + $keno[16];
        $winningNumbers[0] = $sum % 10;
        $sum = $keno[2] + $keno[5] + $keno[8] + $keno[11] + $keno[14] + $keno[17];
        $winningNumbers[1] = $sum % 10;
        $sum = $keno[3] + $keno[6] + $keno[9] + $keno[12] + $keno[15] + $keno[18];
        $winningNumbers[2] = $sum % 10;
        return $winningNumbers;
    }


    public static function index(): void
    {
        //创建新的期号
        PcIssueService::createIssueNo();
        //对该封盘的进行封盘
        PcIssueService:: fengPan();
        //将未开奖的进行开奖
        PcIssueService:: kaiJiang();


    }

    private static function fengPan()
    {
        $list = PcIssue::where('status', PcIssue::STATUS_BETTING)->get();
        $now = time();
        foreach ($list as $item) {
            if (strtotime($item->end_time) - 30 <= $now) {
                $item->status = PcIssue::STATUS_CLOSE;
                $item->save();
            }
        }
    }


    private static function kaiJiang(): void
    {
        $list = PcIssue::where('status', PcIssue::STATUS_CLOSE)->get();
        $now = time();
        foreach ($list as $item) {
            if (strtotime($item->end_time) <= $now) {
                $keno = static::getKeno();
                $item->keno = json_encode($keno);
                $winningNumbers = static::getWinningNumbers($keno);
                $item->winning_numbers = implode(',', $winningNumbers);
                $item->status = PcIssue::STATUS_DRAW;
                $item->save();
                $awards = IssueService::award(explode(',', $item->winning_numbers));

                //预测结果
                PcPrediction::result($item->issue_no, $item->winning_numbers, $awards);
                //自开奖以来的结果统计
                PcCao::updateData($awards);
                //每天开奖结果统计
                PcCaoHistory::updateData($awards);


            }
        }
    }

    private static function createIssueNo()
    {
        $issue = PcIssue::orderByDesc('id')->first();
        $now = time();
        if (strtotime($issue->end_time) <= $now) {
            $issue_no = $issue->issue_no;
            $number = preg_replace('/\D/', '', $issue_no);
            $number = (int)$number + 1;
            $new_str = "P" . $number;
            $end_time = strtotime($issue->end_time) + 210;
            $status = $end_time - 30 > $now ? PcIssue::STATUS_BETTING : PcIssue::STATUS_CLOSE;
            $issue = PcIssue::create([
                'issue_no' => $new_str,
                'status' => $status,
                'start_time' => $issue->end_time,
                'end_time' => date('Y-m-d H:i:s', $end_time),
            ]);
            //预测
            PcPrediction::prediction($new_str);
            if (strtotime($issue->end_time) <= $now) {
                static::createIssueNo();
            }
        }


    }
}