<?php


namespace Heanup\Library\Sms;


use Heanup\App\Api\Package\Database\Num;
use Heanup\Library\Curl;
use Heanup\Library\Sms;
use QL\QueryList;

class ReceiveSmsFree implements Sms
{


    /**
     * @return string[]
     */
    public function getCountry(): array
    {
        $data = [];
        for ($i = 1; $i <= 3; $i++) {
            $url = "https://receive-sms-free.net/regions/" . $i . ".html";
            $ql = (new QueryList)->get($url);
            $range = "#ul> .wow";
            $rules = [
                'country' => ["h2", "text"],
                'link' => ["a", "href"]
            ];
            $_data = $ql->range($range)->rules($rules)->query()->getData()->all();
            if ($_data) {
                $data = array_merge($data, $_data);
            }
        }
        $data = array_map(function ($item) {
            $item['country'] = trim($item['country']);
            return $item;
        }, $data);
        return $data;
    }

    /**
     * @param $link
     * @return array
     */
    public function getPhoneNums($link): array
    {
        $ql = (new QueryList)->get($link);
        $end_page_link = $ql->find(".pagination  a:contains(End)")->attr("href");
        preg_match("#\d+#", $end_page_link, $all_page);
        $all_page = $all_page[0];
        $data = $ql->find(".h2 span")->texts()->all();
        $this->insert($link, $data);
        if ($all_page > 0) {
            for ($i = 2; $i <= $all_page; $i++) {
                $url = sprintf($link . "%s.html", $i);
                $_ql = (new QueryList)->get($url);
                $_data = $_ql->find(".h2 span")->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) {
//            print_r($info);
//            $curl = curl_init();
//            $cookie_jar = '/tmp/sms_cookie';//tempnam('./tmp','cookie'); //tempnam--建立一个具有唯一文件名的文件
//
//            curl_setopt_array($curl, array(
//                CURLOPT_URL => $info['url'],
//            ));
////            curl_setopt($curl, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
////            curl_setopt($curl, CURLOPT_PROXY, "myhome.97admin.com"); //代理服务器地址
////            curl_setopt($curl, CURLOPT_PROXYPORT, 34185); //代理服务器端口
////            curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); //使用http代理模式
//            curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
//
//            $response = curl_exec($curl);
//
//            curl_close($curl);
            $response=Curl::get($info['url'],"myhome.97admin.com:34185");
            $ql = (new QueryList)->setHtml($response);
            $range = ".row:gt(0)";
            $rules = [
                'from' => [".col-xs-12  > .mobile_hide", "text"],
                'time' => [".col-xs-0", "text"],
                'message' => [".col-md-8", "text"]
            ];
            $ql->find(".mobile_hide:contains(ADS)")->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);
            $insert = [
                'country' => $num[0],
                'phone' => $num[1],
                'site' => 'receive-sms-free.net',
                'url' => $link . $_num . "/"
            ];
            Num::insert($insert);
        }
    }
}