<?php

namespace Controller;

use Lib\Mail\Mail;
use Lib\Mail\MailFun;
use Lib\Verify;
use Model\emailSql;
use Model\folderSql;
use Model\listsSql;


/**
 * @author:dc
 * @time 2023/2/13 11:28
 * Class Home
 * @package Controller
 */
class Home extends Base {


    /**
     * 邮件列表
     * @author:dc
     * @time 2023/2/17 14:12
     */
    public function lists(){

        // 分页 页数
        $page   =   app()->request('page',1,'intval');
        $page   =   $page ? $page : 1;

        $limit   =   app()->request('limit',20,'intval');
        $limit   =   $limit ? $limit : 1;

        // 指定id
        $ids = app()->request('mail_id');
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $i=>$d){
            if(!is_numeric($d)){
                unset($ids[$i]);
            }
        }

        // 附件
        $attachment =   app()->request('attachment');
        // 已读/未读
        $seen =   app()->request('seen',-1,'intval');
        // 软删
        $deleted =   app()->request('deleted',0,'intval');


        $where = ['email_id'=>$this->getEmails('id')];

        // 目录
        $folder = app()->request('folder','INBOX');
        $folderList = db()->all(folderSql::all($where['email_id']));
        $folder_id = [];
        // 文件夹id
        if($folderList){
            foreach ($folderList as $item){
                if($item['folder'] == $folder){
                    $folder_id[] = $item['id'];
                }
            }
        }

        //目录
        if($folder_id) $where['folder_id'] = $folder_id;
        if($ids) $where['id'] = $ids;
        if($attachment) $where['is_file'] = 1; //附件
        // 软删
        $where['deleted'] = $deleted;
        // 已读/未读
        if(in_array($seen,[0,1])){
            $where['seen'] = $seen;
        }

        $lists = db()->all(
            listsSql::lists(
                dbWhere($where),
                $page,
                $limit
            )
        );

        // 总数
        $total  = db()->count(
            listsSql::listCount(dbWhere($where))
        );

        app()->_json(listsPage($lists,$total,$page,$limit));

    }



    /**
     * 发送邮件
     * @author:dc
     * @time 2023/2/18 17:32
     */
    public function send_mail(){

        $email = $this->getEmail();

        $formData = Verify::checks([
            'nickname|'.__('nickname')  =>  ['required','max'=>50],
            'subject|'.__('subject')  =>  ['required','max'=>150],
            'body|'.__('body_email')  =>  ['required'],
            'to|'.__('to_email')  =>  ['required','array|string','email'],
            'priority|'.__('priority_email')  =>  ['in'=>[1,3,5]],
            'files|'.__('files_email')  =>  [
                'file'=>[
                    'ext'   =>  [],
                    'size'  =>  500,
                    'mine'  =>  []
                ]
            ],
            'receipt|'.__('receipt_email')  =>  []
        ],[

        ]);

        $ret = MailFun::sendEmail(
            $email['smtp'],
            $email['email'],
            base64_decode($email['password']),
            $formData['nickname']??'',
            $formData['to'],
            $formData['subject'],
            $formData['body'],
            $formData['files']??'',
            $formData['receipt']??'',
            $formData['priority']??3,
        );

        if($ret[0]===true) {
            app()->_json(['messageId'=>$ret[1]]);
        }else {
            app()->e($ret[1]);
        }
    }


    /**
     * 收到前端的同步请求操作
     * @author:dc
     * @time 2023/3/10 10:38
     */
    public function sync(){

        $emails = web_request_emails();

        if(empty($emails)){
            app()->e('sync_request_param_error');
        }else{
            // 查询id
            if(count($emails)===1){
                $emails = $emails[0];
            }
            $datas = db()->all(emailSql::getValues(['email'=>$emails],'`id`,`email`,`pwd_error`'));
            foreach ($datas as $k=>$v){
                if(!$v['pwd_error']){
                    redis()->rPush('sync_email_lists', $v['id']);
                }
                $datas[$k]['have_new'] = redis()->getDel('have_new_mail_'.$v['id']);
            }
            // 返回成功的参数值
            app()->_json($datas);
        }

    }


    /**
     * 标记为已读
     * @throws \Lib\Err
     * @author:dc
     * @time 2023/3/17 16:15
     */
    public function seen_2_unseen(){
        $this->setFlags('seen');
    }



    /**
     * 邮件移动
     * @author:dc
     * @time 2023/3/21 11:41
     */
    public function move(){
        $emails = $this->getEmails();

        $mail_ids = app()->request('mail_ids');
        foreach ($mail_ids as $k=>$id){
            if(!is_numeric($id)){
                unset($mail_ids[$k]);
            }
        }
        // 移动到的文件夹
        $to_folder = app()->request('folder');
        if(empty($to_folder)){
            app()->e('folder_move_error');
        }

        $data  = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
        if($data){
            // 查询邮箱
            $emails = array_column($emails,null,'id');
            $uids = [];
            foreach ($data as $datum){
                if(empty($uids[$datum['email_id']])){
                    $uids[$datum['email_id']][$datum['folder_id']] = [];
                }
                $uids[$datum['email_id']][$datum['folder_id']][] = [
                    'uid'   =>   $datum['uid'],
                    'id'   =>   $datum['id'],
                ];
            }

            foreach ($uids as $eid=>$arr){
                // 查询需要移动的文件夹
                $to_origin_folder = db()->first(folderSql::first(['email_id'=>$eid,'folder'=>$to_folder]));
                if($to_origin_folder){
                    foreach ($arr as $fid=>$uid){
                        // 查询目录
                        $folder = db()->first(folderSql::first($fid));
                        if($folder){
                            // 开始远程
                            $mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);

                            if($mailInstance->login()){
                                // TODO:: 这个过程无法保证原子性。没办法
                                // 先复制
                                $ret = $mailInstance->move(array_column($uid,'uid'),$folder['origin_folder'],$to_origin_folder['origin_folder']);
                                if($ret){
                                    $uret = db()->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>array_column($uid,'id')]));
                                }

                                $mailInstance = null;
                            }

                        }
                        $folder = null;
                    }
                }


            }

        }


        app()->_json([
            'mail_id'    =>  $mail_ids
        ]);
    }




    /**
     * 远程标签
     * @param $d
     * @throws \Lib\Err
     * @author:dc
     * @time 2023/3/21 14:28
     */
    private function setFlags($d){
        $emails = $this->getEmails();

        $mail_ids = app()->request('mail_ids');
        foreach ($mail_ids as $k=>$id){
            if(!is_numeric($id)){
                unset($mail_ids[$k]);
            }
        }
        // 已读或未读
        $fv = (int) app()->request($d);
        $fv = $fv ? 1 : 0;

        $data  = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
        if($data){
            // 查询邮箱
            $emails = array_column($emails,null,'id');
            $uids = [];
            foreach ($data as $datum){
                if(empty($uids[$datum['email_id']])){
                    $uids[$datum['email_id']][$datum['folder_id']] = [];
                }
                $uids[$datum['email_id']][$datum['folder_id']][] = [
                    'uid'   =>   $datum['uid'],
                    'id'   =>   $datum['id'],
                ];
            }

            foreach ($uids as $eid=>$arr){
                foreach ($arr as $fid=>$uid){
                    // 查询目录
                    $folder = db()->first(folderSql::first($fid));
                    if($folder){
                        // 开始远程
                        $mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);

                        if($mailInstance->login()){
                            switch ($d){
                                // 已读 未读
                                case 'seen':{
                                    $mailInstance->seen(array_column($uid,'uid'),$folder['origin_folder'],$fv);
                                    break;
                                }
                                // 回收站,已删 未删,软删
//                                case 'deleted':{
//                                    $mailInstance->recycle(array_column($uid,'uid'),$folder['origin_folder'],$fv);
//                                    break;
//                                }
                            }

                            $mailInstance = null;
                            // 更新数据
                            db()->update(listsSql::$table,[
                                $d  =>  $fv
                            ],dbWhere([
                                'id'    =>  array_column($uid,'id')
                            ]));
                        }

                    }
                    $folder = null;
                }
            }

        }


        app()->_json([
            'mail_id'    =>  $mail_ids
        ]);

    }



}