import IComponentServer from '../IComponentServer';

const store: any = {};

/**
 * class TUIPersonServer
 *
 * TUIGroup 逻辑主体
 */
export default class TUIPersonServer extends IComponentServer {
  public TUICore: any;
  public store: any;
  public currentStore: any = {};
  public storeCallback: any;
  constructor(TUICore: any) {
    super();
    this.TUICore = TUICore;
    this.bindTIMEvent();
    this.store = TUICore.setComponentStore(
      'TUIPerson',
      store,
      this.updateStore.bind(this)
    );
  }

  /**
   * 组件销毁
   */
  public destroyed() {
    this.unbindTIMEvent();
  }

  /**
   * 数据监听回调
   *
   * @param {any} newValue 新数据
   * @param {any} oldValue 旧数据
   */
  updateStore(newValue: any, oldValue: any) {
    this.currentStore.groupList = newValue.groupList;
    this.currentStore.searchGroup = newValue.searchGroup;
  }

  /**
   * /////////////////////////////////////////////////////////////////////////////////
   * //
   * //                                    TIM 事件监听注册接口
   * //
   * /////////////////////////////////////////////////////////////////////////////////
   */

  private bindTIMEvent() {
    this.TUICore.tim.on(
      this.TUICore.TIM.EVENT.GROUP_LIST_UPDATED,
      this.handleGroupListUpdated,
      this
    );
    this.TUICore.tim.on(
      this.TUICore.TIM.EVENT.GROUP_ATTRIBUTES_UPDATED,
      this.handleGroupAttributesUpdated,
      this
    );
  }

  private unbindTIMEvent() {
    this.TUICore.tim.off(
      this.TUICore.TIM.EVENT.GROUP_LIST_UPDATED,
      this.handleGroupListUpdated
    );
    this.TUICore.tim.off(
      this.TUICore.TIM.EVENT.GROUP_ATTRIBUTES_UPDATED,
      this.handleGroupAttributesUpdated
    );
  }

  private handleGroupListUpdated(event: any) {
    this.store.groupList = event.data;
  }
  private handleGroupAttributesUpdated(event: any) {
    const { groupID, groupAttributes } = event.data; // 群组ID // 更新后的群属性
    console.log(groupID, groupAttributes);
  }

  /**
   * /////////////////////////////////////////////////////////////////////////////////
   * //
   * //                                 处理 TIM 接口参数及回调
   * //
   * /////////////////////////////////////////////////////////////////////////////////
   */

  /**
   * 处理异步函数
   *
   * @param {callback} callback 回调函数
   * @returns {Promise} 返回异步函数
   */
  public handlePromiseCallback(callback: any) {
    return new Promise<void>((resolve, reject) => {
      const config = {
        TUIName: 'TUIGroup',
        callback: () => {
          callback && callback(resolve, reject);
        }
      };
      this.TUICore.setAwaitFunc(config.TUIName, config.callback);
    });
  }

  /**
   * 获取 SDK 缓存的好友列表
   * Get friend list from SDK
   *
   * @param {Array<string>} userIDList 用户的账号列表/userID list
   * @returns {Promise}
   */
  public async getFriendList(): Promise<void> {
    return this.handlePromiseCallback(async (resolve: any, reject: any) => {
      try {
        const imResponse = await this.TUICore.tim.getFriendList();
        this.currentStore.friendList = imResponse.data;
        this.currentStore.userIDList =
          this.currentStore.friendList.map((item: any) => item.userID) || [];
        resolve(imResponse);
      } catch (error) {
        reject(error);
      }
    });
  }

  /**
   * 赋值
   * Bind
   *
   * @param {Object} params 使用的数据/params data
   * @returns {Object} 数据/data
   */
  public async bind(params: any, callBack: any) {
    this.currentStore = params;
    await this.getFriendList();

    callBack && callBack(this.currentStore);
    return this.currentStore;
  }
}
