<?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 SmsFree implements Sms
{


    /**
     * @return array|false|string|string[]
     */
    public function getCountry()
    {
        $url = "https://smsfree.cc/api/";
        $data = [
            'do' => "Home"
        ];
        $result = Curl::post($url, $data);
        $result = json_decode($result, true);
        return $result['Country'];
    }

    /**
     * @param $link
     * @return array
     */
    public function getPhoneNums($link): array
    {

        $url = "https://smsfree.cc/api/";
        $data = [
            'country' => $link,
            'do' => "List"
        ];
        $result = Curl::post($url, $data);
        $result = json_decode($result, true);
        $response = $result['Phone'];
        $this->insert($link, $result['Phone']);
        $total = $result['End'];
        for ($i = 2; $i <= $total; $i++) {
            $temp = $data;
            $temp['page'] = $i;
            $result = Curl::post($url, $temp);
            $result = json_decode($result, true);
            if ($result) {
                $this->insert($link, $result['Phone']);
                $response = array_merge($response, $result['Phone']);
            }

        }
        return $response;

//
//        $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
    {
        $url = "https://smsfree.cc/api/";
        $_num = str_replace(['+', ' '], ['', ''], $country . $phone_num);
        $data = [
            'page' => 1,
            'phone' => $_num,
            'do' => "Message",
        ];
        $result = Curl::post($url, $data);
        $result = json_decode($result, true);
        $response = array_map(function ($a) {
            $rtime = date("m-d H:i", $a['time']);
            $htime = date("H:i", $a['time']);
            $time = time() - $a['time'];
            if ($time < 60) {
                $str = '刚刚';
            } elseif ($time < 60 * 60) {
                $min = floor($time / 60);
                $str = $min . '分钟前';
            } elseif ($time < 60 * 60 * 24) {
                $h = floor($time / (60 * 60));
                $str = $h . '小时前 ';
            } elseif ($time < 60 * 60 * 24 * 3) {
                $d = floor($time / (60 * 60 * 24));
                if ($d == 1) {
                    $str = '昨天 ' . $htime;
                } else {
                    $str = '前天 ' . $htime;
                }
            } else {
                $str = $rtime;
            }
            return [
                'from' => $a['from'],
                'time' => $str,
                'message' => $a['data'],
            ];
        }, $result['Message']);
        return $response;
    }

    private function insert($link, $data)
    {
        foreach ($data as $val) {
            echo sprintf("正在写入号码：\t\t %s", $val['format']) . PHP_EOL;
            $num = explode(" ", $val['format']);
//            $_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' => 'smsfree.cc',
                'url' => ""
            ];
            Num::insert($insert);
        }
    }
}