<?php

namespace Lib\Imap\Parse;

use Lib\Imap\DataArray;

/**
 * 邮件内容
 * @author:dc
 * @time 2024/9/20 14:57
 * Class Body
 * @package Lib\Imap\Parse
 */
class Body {

    /**
     * 原始数据
     * @var string
     */
    protected string $raw = '';

    /**
     * 消息结构,解析后的邮件体 header
     * @var MessageItem
     */
    protected MessageItem $msg;


    /**
     * 解析后的body数据
     * @var DataArray[]
     */
    private array $items = [];

    /**
     * Body constructor.
     * @param string $result
     * @param MessageItem $msg
     */
    public function __construct(string $result, MessageItem $msg)
    {
        $this->raw = $result;

        $this->msg = $msg;

        $this->parse();
    }



    /**
     * 解析
     * @author:dc
     * @time 2024/9/14 17:35
     */
    protected function parse(){
        // 是否是多段
        $boundary = $this->msg->header->getBoundary();
        if($boundary){
            // 切割成块
            $items = explode($boundary,$this->raw);
            // 第一个块和最后一块 是没用的块
            array_shift($items);array_pop($items);
            foreach ($items as $item){
                $this->items[] = $this->parseItem($item);
            }
        }


    }

    /**
     * 解析每个 块
     * @param string $body 块字符串
     * @return DataArray
     * @author:dc
     * @time 2024/9/21 9:51
     */
    protected function parseItem(string $body):DataArray {
        list($mime_header,$text) = explode("\r\n\r\n",$body,2);

        $data = $this->parseMimeHeader($mime_header);


        // 处理body体 的编码
        switch ($data->get('Content-Transfer-Encoding')){
            case 'quoted-printable':{
                $text = quoted_printable_decode($text);break;
            }
            case 'base64':{
                $text = base64_decode($text);break;
            }
        }

        $data->body = $text;

        return $data;

    }

    /**
     * 解析邮件体里面的每个块 头部
     * @param string $header
     * @return DataArray
     * @author:dc
     * @time 2024/9/21 9:18
     */
    protected function parseMimeHeader(string $header):DataArray {
        // 处理 描述信息,
        $header = explode("\r\n",trim($header));
        $data = new DataArray();
        $name = '';
        foreach ($header as $head){
            // 判断是否是上一行的
            if(str_starts_with($head,' ') || str_starts_with($head,"\t")){
                $data->set($name,' '.$head,true);
            }else{
                list($name,$value) = explode(":",$head,2);
                $data->set($name,trim($value));
            }
        }

        // 处理编码
        if($data->get('Content-Type')){
            // 切割成 每个小块 Content-Type: text/html;charset=utf-8
            $contentType = explode(' ',trim(str_replace(';',' ',$data->get('Content-Type'))));
            foreach ($contentType as $ct){
                $ct = trim($ct);
                if(str_contains($ct,'/')){
                    $data->set('Content-Type',$ct);
                }elseif (str_contains($ct,'=')){
                    list($name,$val) = explode('=',$ct);
                    $data->set($name,trim($val));
                }
            }
        }

        // 默认编码
        if(!$data->Charset) $data->Charset = 'utf-8';

        return $data;
    }


    /**
     * 读取纯文本的内容
     * @author:dc
     * @time 2024/9/21 9:55
     */
    public function getText():string {
        foreach ($this->items as $item){
            if($item->eq('content-type','text/plain')){
                return $item->body;
            }
        }

        // 没找到 text
        return strip_tags($this->getHtml());

    }

    /**
     * 读取 html文本
     * @return string
     * @author:dc
     * @time 2024/9/21 10:02
     */
    public function getHtml():string {
        foreach ($this->items as $item){
            if($item->eq('content-type','text/html')){
                return $item->body;
            }
        }
    }

    /**
     * 有些邮件里面的图片是通过附件的形式发来的
     * <img src="cid:xxxx" /> 这种就是附件图片,需要替换的
     * @return array
     * @author:dc
     * @time 2024/9/21 10:53
     */
    public function getHtmlAndImg():array {
        $html = $this->getHtml();

    }

    /**
     * 读取附件
     * @return DataArray[]
     * @author:dc
     * @time 2024/9/21 10:05
     */
    public function getAttachment():array {

    }



}