<?php


namespace Heanup\Library\Sms;


use Heanup\App\Api\Config\Code;
use Heanup\App\Api\Package\Database\Num;
use Heanup\Config\Proxy;
use Heanup\Library\Curl;
use QL\QueryList;

class ReceiveSmsCc implements Sms
{


    /**
     * @return string[]
     */
    public function getCountry(): array
    {
        $url = "https://receive-sms.cc/Countries/";
        $ql = (new QueryList)->get($url);
        $range = ".number-boxes-item";
        $rules = [
            'country' => [".number-boxes-item-country", "text"],
            'img' => [".number-boxes-item-ico", "src"],
            'link' => [".number-boxes-item-button", "href"]
        ];

        $data = $ql->range($range)->rules($rules)->query()->getData()->all();
        $data = array_map(function ($a) {
            $a['link'] = "https://receive-sms.cc" . $a['link'];
            $a['img'] = "https://receive-sms.cc" . $a['img'];
            return $a;
        }, $data);

        return $data;
    }

    /**
     * @param $link
     * @return array
     */
    public function getPhoneNums($link): array
    {
        $ql = (new QueryList)->get($link);
        $data = $ql->find(".number-boxes-item-number")->texts()->all();
        if ($data) {
            $this->insert($link, $data);
            $end_page_link = $ql->find(".pagination  a:contains(End)")->attr("href");
            preg_match("#\d+#", $end_page_link, $all_page);
            $all_page = $all_page[0];
            if ($all_page > 0) {
                for ($i = 2; $i <= $all_page; $i++) {
                    $url = sprintf($link . "Page/%s", $i);
                    $_ql = (new QueryList)->get($url);
                    $_data = $_ql->find(".number-boxes-item-number")->texts()->all();
                    if ($_data) {
                        $this->insert($link, $_data);
                        $data = array_merge($data, $_data);
                    } else {
                        echo "总页数：" . $all_page;
                        echo "处理异常: " . $url . PHP_EOL;
                    }
                }
            }
        }

        return $data;
    }

    public function getMessage($country, $phone_num): array
    {
        $info = Num::getOne(['country' => $country, 'phone' => $phone_num]);
        if ($info) {
            $response = Curl::get($info['url'],Proxy::getData());
            $ql = (new QueryList)->setHtml($response);
            $range = ".table-hover";
            $rules = [
                'from' => [".col-xs-12  > .mobile_hide", "text"],
                'time' => [".col-xs-0", "text"],
                'message' => [".col-md-8", "text"]
            ];
            $ql->find(".mobile_hide:contains(Google)")->parent()->parent()->remove();
            $msgs = $ql->range($range)->rules($rules)->query()->getData()->all();
            return $msgs;
        }
    }

    private function insert($link, $data)
    {
        foreach ($data as $val) {
            echo sprintf("正在写入号码：\t\t %s", $val) . PHP_EOL;
            $num = explode(" ", $val);
            $_num = str_replace(['+', ' '], ['', ''], $val);
            $config = Code::getData($num[0]);
            $insert = [
                'country' => $num[0],
                'country_name_zh' => $config['cn'],
                'country_name_en' => $config['en'],
                'phone' => $num[1],
                'site' => 'receive-sms.cc',
                'url' => $link . $_num
            ];
            Num::insert($insert);
        }
    }
}