import { getMillis } from "taehui-ts/date";
import { wwwAXIOS } from "src/Www";
import { EssayAPIEssay, GetAutoEssayAPI, GetCommentAPI } from "src/wwwAPI";

export default function setForumStore() {
  return {
    avatarID: "",
    avatarName: "",
    forumTitle: "",
    title: "",
    text: "",
    hitCount: 0,
    comments: [] as GetCommentAPI,
    essayBefore: undefined as EssayAPIEssay | undefined,
    essayLater: undefined as EssayAPIEssay | undefined,
    autoEssayID: undefined as number | undefined,
    autoEssays: [] as GetAutoEssayAPI,

    lastPage: 0,
    pageUnit: 5,
    viewUnit: 10,
    level: 0,

    setAvatarID(avatarID: string) {
      this.avatarID = avatarID;
    },

    setAvatarName(avatarName: string) {
      this.avatarName = avatarName;
    },

    setForumTitle(forumTitle: string) {
      this.forumTitle = forumTitle;
    },

    setTitle(title: string) {
      this.title = title;
    },

    setText(text: string) {
      this.text = text;
    },

    setHitCount(hitCount: number) {
      this.hitCount = hitCount;
    },

    setComments(comments: GetCommentAPI) {
      this.comments = comments;
    },

    setEssayBefore(essayBefore: EssayAPIEssay) {
      this.essayBefore = essayBefore;
    },

    setEssayLater(essayLater: EssayAPIEssay) {
      this.essayLater = essayLater;
    },

    setAutoEssayID(autoEssayID: number) {
      this.autoEssayID = autoEssayID;
    },

    setLastPage(lastPage: number) {
      this.lastPage = lastPage;
    },

    setLevel(level: number) {
      this.level = level;
    },

    async setAutoEssays(forumID: string, totem: string) {
      const { status, data } = await wwwAXIOS.get<GetAutoEssayAPI>(
        `/autoEssay/${forumID}`,
        {
          headers: {
            millis: getMillis(),
            totem,
          },
        },
      );
      if (status === 200) {
        this.autoEssays = data;
        return true;
      } else {
        return false;
      }
    },
  };
}
