<?php
namespace app\common\library;

use Easemob\Auth;
use Easemob\Group;
use Easemob\User;
use Easemob\Room;

class Easemob {

    protected $auth;
    protected $room;
    protected $user;
    protected $group;

    public function __construct() {

        //初始化配置
        $easemob_config               = config('easemob');
        $appKey                       = $easemob_config['appkey'];
        $clientIdOrAppID              = $easemob_config['client_id'];
        $clientSecretOrAppCertificate = $easemob_config['client_secret'];

        $this->auth = new Auth($appKey,$clientIdOrAppID,$clientSecretOrAppCertificate);
        $this->room = new Room($this->auth);
        $this->user = new User($this->auth);
        $this->group = new Group($this->auth);
    }


    /////////////////////////////////管理用户/////////////////////////////

    //注册用户
    /*
        array(6) {
            ["uuid"] => string(36) "9783ddc0-7eeb-11ee-bbeb-913291843983"
            ["type"] => string(4) "user"
            ["created"] => int(1699526081953)
            ["modified"] => int(1699526081953)
            ["username"] => string(2) "12"
            ["activated"] => bool(true)
        }
    */
    public function user_create($user_id){

        $user_id = '' . $user_id . '';
        $register_data = ['username'=>$user_id,'password'=>123456];

        $rs = $this->user->create($register_data);
        //dump($rs);
        if(isset($rs['code'])){
            return false;
        }
        if(isset($rs['uuid'])){
            return true;
        }
        //默认
        return false;
    }


    /////////////////////////////////管理群组/////////////////////////////

    //创建群组，返回群组id
    public function group_createPublicGroup($user_id,$guild_name,$guild_desc,$maxusers){
        $user_id = ''.$user_id.'';
        $rs = $this->group->createPublicGroup($user_id,$guild_name,$guild_desc,[],$maxusers);
        return $rs;
    }

    //添加一个群组成员
    //$rs === true;
    public function group_addGroupMember($groupId,$user_id){
        $rs = $this->group->addGroupMember($groupId,$user_id);
        return $rs;
    }

    //移除一个群组成员
    public function group_removeGroupMember($groupId,$user_id)
    {
        $rs = $this->group->removeGroupMember($groupId,$user_id);
        return $rs;
    }

    //添加一个管理员
    public function group_addGroupAdmin($groupId,$user_id){
        $rs = $this->group->addGroupAdmin($groupId,$user_id);
        return $rs;
    }

    //移除一个管理员
    public function group_removeGroupAdmin($groupId,$user_id){
        $rs = $this->group->removeGroupAdmin($groupId,$user_id);
        return $rs;
    }

    /////////////////////////////////管理聊天室/////////////////////////////
    //https://docs-im-beta.easemob.com/document/server-side/chatroom.html

    //获取 app 中的聊天室

    /**
     * 获取用户所加入的聊天室
     *
        array(2) {
            [0] => array(3) {
                ["id"] => string(15) "230909536174085"
                ["name"] => string(15) "聊天室名称"
                ["disabled"] => string(5) "false"
            }
            [1] => array(3) {
                ["id"] => string(15) "230909515202562"
                ["name"] => string(15) "888888的房间"
                ["disabled"] => string(5) "false"
            }
        }
     */
    public function room_listAllRoomsUserJoined($user_id){
        $user_id = '' . $user_id . '';
        $rs = $this->room->listAllRoomsUserJoined($user_id);
        return $rs;
    }

    //查询聊天室详情
    /*
    array(14) {
        ["id"] => string(15) "230909515202562"
        ["name"] => string(15) "888888的房间"
        ["description"] => string(15) "888888的房间"
        ["membersonly"] => bool(false)
        ["allowinvites"] => bool(false)
        ["maxusers"] => int(1000)
        ["owner"] => string(1) "2"
        ["created"] => int(1699610874211)
        ["custom"] => string(0) ""
        ["mute"] => bool(false)
        ["affiliations_count"] => int(1)
        ["disabled"] => bool(false)
        ["affiliations"] => array(1) {
            [0] => array(1) {
                ["owner"] => string(1) "2"
            }
        }
        ["public"] => bool(true)
    }
    */
    public function room_getRoom($roomId){
        $rs = $this->room->getRoom($roomId);
        return $rs;
    }

    //创建聊天室
    /*
        string(15) "230821166383116"
     */
    public function room_createRoom($name,$info,$user_id){
        $user_id = '' . $user_id . '';
        $room_id = $this->room->createRoom($name,$info,$user_id,[$user_id],10000);
        return $room_id;
    }

    //修改聊天室信息
    /*
        bool(true)
     */
    public function room_updateRoom($room_id,$name,$description,$maxusers = false){
        $data = [
            'room_id' => $room_id,
            'name'    => $name,
            'description'=>$description,
        ];
        if($maxusers !== false){
            $data['maxusers'] = $maxusers;
        }
        $rs = $this->room->updateRoom($data);
        return $rs;
    }

    //删除聊天室
    //获取聊天室公告
    //修改聊天室公告
    //设置聊天室自定义属性

    //获取聊天室自定义属性
    /*
    array(14) {
        ["seat4"] => string(3) "555"
        ["seat3"] => string(3) "555"
        ["seat2"] => string(3) "555"
        ["seat1"] => string(3) "555"
        ["seat8"] => string(3) "555"
        ["sate1"] => string(1) "5"
        ["seat7"] => string(3) "555"
        ["sate2"] => string(1) "6"
        ["seat6"] => string(3) "555"
        ["seat5"] => string(3) "555"
        ["sate3"] => string(1) "7"
        ["seat9"] => string(3) "555"
        ["sate4"] => string(1) "8"
        ["seat10"] => string(3) "555"
    }
    */
    public function room_getRoomCustomAttribute($room_id,$keys){
        $rs = $this->room->getRoomCustomAttribute($room_id,$keys);
        return $rs;
    }
    //删除聊天室自定义属性

    //强制设置聊天室自定义属性
    //初次设置新增，或修改都可用，一次请求的数组大小为10，可多次设置，共100个
    /*
        array(2) {
          ["successKeys"] => array(10) {
            [0] => string(5) "seat1"
            [1] => string(5) "seat2"
            [2] => string(5) "seat3"
            [3] => string(5) "seat4"
            [4] => string(5) "seat5"
            [5] => string(5) "seat6"
            [6] => string(5) "seat7"
            [7] => string(5) "seat8"
            [8] => string(5) "seat9"
            [9] => string(6) "seat10"
          }
          ["errorKeys"] => array(0) {
          }
        }
     */
    public function room_setRoomCustomAttributeForced($room_id,$owner_id,$matedata){
        $owner_id = ''.$owner_id.'';
        //先进入房间
        $rs = $this->room->addRoomMember($room_id,$owner_id);
        //dump($rs);
        //再设置
        $rs = $this->room->setRoomCustomAttributeForced($room_id,$owner_id,$matedata);
        return $rs;
    }

    //强制删除聊天室自定义属性
    /*
        array(2) {
            ["successKeys"] => array(1) {
                [0] => string(5) "sate1"  //被删除的keys
            }
            ["errorKeys"] => array(0) {
            }
        }
        //再次删除同一个
        array(2) {
            ["successKeys"] => array(0) {
            }
            ["errorKeys"] => array(1) {
                ["sate1"] => string(33) "properties key sate1 is not exist"
            }
        }
    */
    public function room_deleteRoomCustomAttributeForced($room_id,$owner_id,$keys){
        $owner_id = ''.$owner_id.'';
        $rs = $this->room->deleteRoomCustomAttributeForced($room_id,$owner_id,$keys);
        return $rs;
    }

    ///////////////管理聊天室成员//////////////////

    //分页获取聊天室成员列表
    public function room_listRoomMembers($roomId, $pageSize = 10, $pageNum = 1){
        $rs = $this->room->listRoomMembers($roomId, $pageSize, $pageNum);
        return $rs;
    }
    //添加单个聊天室成员
    //批量添加聊天室成员
    //移除单个聊天室成员
    //批量移除聊天室成员
    public function room_removeRoomMembers($roomId, $usernames){
        $rs = $this->room->removeRoomMembers($roomId, $usernames);
        return $rs;
    }
    //获取聊天室管理员列表
    //添加聊天室管理员
    public function room_promoteRoomAdmin($roomId, $newadmin){
        $newadmin = ''.$newadmin.'';
        $rs = $this->room->promoteRoomAdmin($roomId, $newadmin);
        //dump($rs);
        return $rs;
    }
    //移除聊天室管理员
    public function room_demoteRoomAdmin($roomId, $oldadmin){
        $oldadmin = ''.$oldadmin.'';
        $rs = $this->room->demoteRoomAdmin($roomId, $oldadmin);
        //dump($rs);
        return $rs;
    }


    //////////////////管理黑名单///////////
    //////////////////管理白名单///////////
    //////////////////管理禁言///////////



}