import { defineComponent } from "vue";
import { ElButton } from "element-plus";
import runtime, * as RuntimeUtils from "/src/components/live-broadcast/runtime";
import styles from "./message.module.less";
import { state } from "/src/state";
import request from "/src/helpers/request";

type optionsType = "ALL" | "JOIN";
export default defineComponent({
  name: "message",
  data() {
    return {
      options: "ALL" as optionsType,
    };
  },
  mounted() {
    const config = state.user?.roomConfig
      ? JSON.parse(state.user?.roomConfig)
      : {};
    runtime.allowChatCtrl = config.whether_chat === 0;
    runtime.allowSeatsCtrl = config.whether_mic === 0;
  },
  methods: {
    async SeatsCtrl() {
      try {
        const users = state.user;
        const roomConfig = users?.roomConfig
          ? JSON.parse(users?.roomConfig)
          : {};
        await request.post("/api-web/imLiveBroadcastRoom/update", {
          requestType: "json",
          data: {
            id: users.id,
            liveRemark: users.liveRemark,
            liveStartTime: users.liveStartTime,
            preTemplate: users.preTemplate,
            popularizeType: users.popularizeType,
            roomConfig: {
              whether_chat: roomConfig.whether_chat,
              whether_like: roomConfig.whether_like,
              whether_mic: runtime.allowSeatsCtrl ? 1 : 0,
              whether_video: roomConfig.whether_video,
            },
            roomTitle: users.roomTitle,
            speakerId: users.speakerId,
            tenantId: users.tenantId,
          },
        });
        state.user.roomConfig = JSON.stringify({
          whether_chat: roomConfig.whether_chat,
          whether_like: roomConfig.whether_like,
          whether_mic: runtime.allowSeatsCtrl ? 1 : 0,
          whether_video: roomConfig.whether_video,
        });

        await RuntimeUtils.sendMessage(
          {
            seatBan: runtime.allowSeatsCtrl,
            ...RuntimeUtils.getSendMessageUser(),
          },
          "SeatsCtrl"
        );
        runtime.allowSeatsCtrl = !runtime.allowSeatsCtrl;
      } catch {
        //
      }
    },
    async ChatBan() {
      try {
        const users = state.user;
        const roomConfig = users?.roomConfig
          ? JSON.parse(users?.roomConfig)
          : {};
        await request.post("/api-web/imLiveBroadcastRoom/update", {
          requestType: "json",
          data: {
            id: users.id,
            liveRemark: users.liveRemark,
            liveStartTime: users.liveStartTime,
            preTemplate: users.preTemplate,
            popularizeType: users.popularizeType,
            roomConfig: {
              whether_chat: runtime.allowChatCtrl ? 1 : 0,
              whether_like: roomConfig.whether_like,
              whether_video: roomConfig.whether_video,
              whether_mic: roomConfig.whether_mic,
            },
            roomTitle: users.roomTitle,
            speakerId: users.speakerId,
            tenantId: users.tenantId,
          },
        });
        state.user.roomConfig = JSON.stringify({
          whether_chat: runtime.allowChatCtrl ? 1 : 0,
          whether_like: roomConfig.whether_like,
          whether_video: roomConfig.whether_video,
          whether_mic: roomConfig.whether_mic,
        });
        await RuntimeUtils.sendMessage(
          {
            chatBan: runtime.allowChatCtrl,
            ...RuntimeUtils.getSendMessageUser(),
          },
          "ChatBan"
        );
        runtime.allowChatCtrl = !runtime.allowChatCtrl;
      } catch {
        //
      }
    },
  },
  render() {
    return (
      <div class={styles.message}>
        <div class={styles.buttonGroup}>
          {/* {this.whetherChat ? <ElButton type={!runtime.allowChatCtrl ? 'primary' : 'info'} onClick={this.ChatBan} >{runtime.allowChatCtrl ? '全体禁言' : '关闭全体禁言'}</ElButton> : null} */}
          <ElButton
            type={!runtime.allowChatCtrl ? "primary" : "info"}
            onClick={this.ChatBan}
          >
            {runtime.allowChatCtrl ? "关闭聊天" : "开启聊天"}
          </ElButton>
          <ElButton
            type={!runtime.allowSeatsCtrl ? "primary" : "info"}
            onClick={this.SeatsCtrl}
          >
            {runtime.allowSeatsCtrl ? "关闭连麦" : "开启连麦"}
          </ElButton>
        </div>
      </div>
    );
  },
});
