<?php


namespace Heanup\Library\Sms;


use Heanup\App\Api\Config\Code;
use Heanup\App\Api\Package\Database\Num;
use Heanup\Library\Curl;
use Heanup\Library\Redis;
use PhpImap\Mailbox;
use QL\QueryList;

class GoogleVoice implements Sms
{


    /**
     * @return string[]
     */
    public function getCountry(): array
    {
        return [];
    }

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


        return [];
    }

    public function getMessage($country, $phone_num): array
    {
        $mailbox = new Mailbox(
            '{mail.97admin.com:993/imap/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
            "gv@a4sky.com", // Username for the before configured mailbox
            "6485882", // Password for the before configured username
            __DIR__, // Directory, where attachments will be saved (optional)
            'UTF-8' // Server encoding (optional)
        );
        $mail_ids = $mailbox->searchMailbox('FROM txt.voice.google.com');
        rsort($mail_ids);
        $cache = Redis::instance();
        $i = 0;
        krsort($mail_info);
        foreach ($mail_ids as $mail_id) {
            if ($i >= 20) {
                break;
            } else {
                $key = "mails_gv:" . $mail_id;
                $temp = $cache->get($key);
                if (!$temp) {
                    $detail = $mailbox->getMail($mail_id);
                    $from_num = str_replace("Message from ", "", $detail->subject);
                    $txt = $detail->textPlain;
                    $rwn = explode("\r\n", $detail->headersRaw);
                    $date = explode(": ", $rwn[2])[1];
                    $temp = [
                        'from' => $from_num,
                        'time' => date("Y-m-d H:i:s", strtotime($date)),
                        'message' => $txt
                    ];
                    $cache->set($key, $temp);
                }
                $mail_info[] = $temp;
            }
            $i++;
        }
//        rsort($mail_info);
        return $mail_info;
    }

}