<?php

/**
 * Class SessionController
 * @author Blackbinbin
 */
class SessionController extends BaseController {
    protected $ajaxLoginActions = [
        'list',
        'personMsg',
        'sendMsg',
        'mute',
        'unMute',
        'pin',
        'unPin',
    ];

    public function __construct() {
        parent::__construct(true);
    }

    /**
     * 返回会话列表
     * @author benzhan
     */
    public function actionList($args) {
        $rules = [
//            'update_time_int' => ['int', 'nullable' => true, 'desc' => '最后更新时间']
            'current_group_id' => ['int', 'nullable' => true, 'desc' => '当前群id'],
        ];
        Param::checkParam2($rules, $args);

        $user_id = User::getUserId();
//        $update_time_int = (int) $args['update_time_int'];

        $where = compact('user_id');
        $keyWord = [
            '_field' => 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute, read_num, update_time_int',
            '_sortKey' => 'pin_time_int DESC, update_time_int DESC',
            '_limit' => 500, // 最多加载500个会话
        ];

        $objSession = new Session();
//        if ($update_time_int) {
//            // 只加载新更新的
//            $keyWord['_where'] = "update_time_int > {$update_time_int}";
//        }

        $currentGroupId = intval($args['current_group_id']);
        $currentGroupId && $where['is_group'] = 0; // 群拉人列表过滤群session

        $list = $objSession->objTable->getAll($where, $keyWord);
        $list = $objSession->fillSession($user_id, $list, $currentGroupId);

        return $list;
    }

    private function _updateState($session_id, $newData) {
        $user_id = User::getUserId();
        $objSession = new Session();
        try {
            // 私聊
            if ($session_id != intval($session_id)) {
                $objSession->checkPersonSession($user_id, $session_id);
            } else {
                $objSession->checkGroupSession($user_id, $session_id);
            }

            $newData['update_time_int'] = microtime(true) * 1000;
            $objSession->updateState($session_id, $newData, compact('user_id'));
        } catch (Exception $ex) {
            Response::error($ex->getCode(), $ex->getMessage());
        }
    }

    private function _delete($session_id) {
        $user_id = User::getUserId();
        $objSession = new Session();
        try {
            // 私聊
            if ($session_id != intval($session_id)) {
                $objSession->checkPersonSession($user_id, $session_id);
            } else {
                $objSession->checkGroupSession($user_id, $session_id);
            }

            $objSession->objTable->delObject(compact('user_id', 'session_id'));
        } catch (Exception $ex) {
            Response::error($ex->getCode(), $ex->getMessage());
        }
    }

//    /**
//     * 设置已读
//     * @author benzhan
//     */
//    public function actionSetRead($args) {
//        $rules = [
//            'session_id' => ['string', 'desc' => '会话id'],
//            'hash' => ['string', 'desc' => '消息Hash值'],
//        ];
//        Param::checkParam2($rules, $args);
//
//        $this->_updateState($args['session_id'], ['read_hash' => $args['hash']]);
//    }

    /**
     * 静音
     * @author benzhan
     */
    public function actionMute($args) {
        $rules = [
            'session_id' => ['string', 'desc' => '会话id'],
        ];
        Param::checkParam2($rules, $args);

        $this->_updateState($args['session_id'], ['is_mute' => 1]);
    }

    /**
     * 取消静音
     * @author benzhan
     */
    public function actionUnMute($args) {
        $rules = [
            'session_id' => ['string', 'desc' => '会话id'],
        ];
        Param::checkParam2($rules, $args);

        $this->_updateState($args['session_id'], ['is_mute' => 0]);
    }

    /**
     * 置顶
     * @author benzhan
     */
    public function actionPin($args) {
        $rules = [
            'session_id' => ['string', 'desc' => '会话id'],
        ];
        Param::checkParam2($rules, $args);

        $this->_updateState($args['session_id'], ['is_pin' => 1, 'pin_time_int' => time()]);
    }

    /**
     * 取消置顶
     * @author benzhan
     */
    public function actionUnPin($args) {
        $rules = [
            'session_id' => ['string', 'desc' => '会话id'],
        ];
        Param::checkParam2($rules, $args);

        $this->_updateState($args['session_id'], ['is_pin' => 0, 'pin_time_int' => 0]);
    }

    /**
     * 删除会话
     * @author benzhan
     */
    public function actionDelete($args) {
        $rules = [
            'session_id' => ['string', 'desc' => '会话id'],
        ];
        Param::checkParam2($rules, $args);

        $this->_delete($args['session_id']);
    }
//
//    /**
//     * 文件上传
//     * @author solu
//     * @param $args
//     * @return array
//     */
//    public function actionUploadFile($args) {
//        $args = array_merge($args, $_FILES);
//        $rules = [
//            'res' => ['array', 'desc' => '资源文件'],
//        ];
//        Param::checkParam2($rules, $args);
//
//        $url = '';
//        try {
//            $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
//        } catch (Exception $e) {
//            Response::error($e->getCode(), $e->getMessage());
//        }
//
//        return compact('url');
//    }

}

