import IMFactory from "./IMFactory";
import axios from "axios";
import LibGenerateUserSig from './lib-generate-test-usersig-es.min'

class IM {
  constructor(spec) {
    this.sp = (spec && spec.sp) || "tencent";
    this.client = null;
    this.IMObject = null;
    this.isConnected = false;
  }

  async init(params) {
    if (this.sp === "goeasy".toLowerCase()) {
      const options = {
        id: params.userID || '123',
        host: params.token.host,
        appkey: params.token.appkey,
        modules: params.modules || ["im"],
        data: { "avatar": params.avatar, "nickname": params.nickname }
      };
      this.IMObject = IMFactory.getInstance(this.sp, options);
    } else if (this.sp === "tencent") {
      const tmp = {
        appkey: params.token?.SECRETKEY || "31205886d047e20e20627b9526dc664a3af342f04afccdb21217b6065f915157",
        appID: params.token?.SDKAppID || 1400539238,
        expire: params.expire || 604800,
        userID: params.userID
      }
      const options = {
        userID: tmp.userID,
        userSig: this.genTestUserSig(tmp),
        SDKAppID: tmp.appID,
        SECRETKEY: tmp.appkey,
        EXPIRETIME: tmp.expire,
      };
      this.IMObject = IMFactory.getInstance(this.sp, options);
    }
  }

  async disconnect() {
    if (this.isConnected) {
      this.IMObject.disconnect()
    }
  }

  async connect() {
    if (this.isConnected) {
      console.warn("duplicate RtcClient.join() observed");
      return;
    }
    try {
      await this.IMObject.connect(() => {
        this.isConnected = true;
      });
      this.isConnected = true;
      console.log('connected :>> ');
    } catch (error) {
      console.error("connect failed! " + error);
    }
  }

  async createGroup(param) {
    try {
      return await this.IMObject.createGroup(param);
    } catch (error) {
      console.error("createGroup failed! " + error);
    }
  }

  async getGroupList() {
    try {
      return await this.IMObject.getGroupList();
    } catch (error) {
      console.error("getGroupList failed! " + error);
    }
  }

  async joinGroup(param) {
    try {
      await this.IMObject.joinGroup(param);
    } catch (error) {
      console.error("joinGroup failed! " + error);
    }
  }

  async leaveGroup(groupId) {
    try {
      await this.IMObject.leaveGroup(groupId);
    } catch (error) {
      console.error("leaveGroup failed! " + error);
    }
  }

  async dismissGroup(groupId) {
    try {
      await this.IMObject.dismissGroup(groupId);
    } catch (error) {
      console.error("dismissGroup failed! " + error);
    }
  }

  async sendMessage(message) {
    try {
      await this.IMObject.sendMessage(message);
    } catch (error) {
      console.error("sendMessage failed! ",error);
    }
  }

  async markMessageRead(message) {
    try {
      await this.IMObject.markMessageRead(message);
    } catch (error) {
      console.error("markMessageRead failed! " + error);
    }
  }

  async revokeMessage(message) {
    try {
      await this.IMObject.revokeMessage(message);
    } catch (error) {
      console.error("revokeMessage failed! " + error);
    }
  }

  async resendMessage(message) {
    try {
      await this.IMObject.resendMessage(message);
    } catch (error) {
      console.error("resendMessage failed! " + error);
    }
  }

  async deleteMessage(message) {
    try {
      await this.IMObject.deleteMessage(message);
    } catch (error) {
      console.error("deleteMessage failed! " + error);
    }
  }

  async getMessageList(param) {
    try {
      return await this.IMObject.getMessageList(param);
    } catch (error) {
      console.error("getMessageList failed! " + error);
    }
  }

  async subscribeGroups(groups, callback){
    try {
      await this.IMObject.subscribeGroup(groups, (message)=>{
        callback(message)
      });
    } catch (error) {
      console.error("subscribeGroup failed! " + error);
    }
  }

  async ApiRequest(url, data) {
    try {
      return await axios({
        method: "post",
        url: url,
        data: data,
      });
    } catch (error) {
      console.log(error);
    }
  }

  genTestUserSig = (params) => {
    let generator = new LibGenerateUserSig(
      params.appID,
      params.appkey,
      params.expire
    );
    let userSig = generator.genTestUserSig(params.userID);
    return userSig;
  };
}

export default IM;
