import i18next from "i18next";
import { PluginSettingTab, Setting } from "obsidian";
import type HaloPlugin from "./main";
import { HaloSitesModal } from "./sites-modal";

export interface HaloSite {
  name: string;
  url: string;
  token: string;
  default: boolean;
}

export interface ImageUploadSettings {
  enabled: boolean;
  uploadPath: string;
  preserveOriginal: boolean;
}

export interface HaloSetting {
  sites: HaloSite[];
  publishByDefault: boolean;
  imageUpload: ImageUploadSettings;
}

export const DEFAULT_SETTINGS: HaloSetting = {
  sites: [],
  publishByDefault: false,
  imageUpload: {
    enabled: true,
    uploadPath: "",
    preserveOriginal: false,
  },
};

export class HaloSettingTab extends PluginSettingTab {
  constructor(private readonly plugin: HaloPlugin) {
    super(app, plugin);
  }

  display() {
    const { containerEl } = this;

    containerEl.empty();

    new Setting(containerEl)
      .setName(i18next.t("settings.site.name"))
      .setDesc(i18next.t("settings.site.description"))
      .addButton((button) =>
        button.setButtonText(i18next.t("settings.site.actions.open")).onClick(() => {
          new HaloSitesModal(this.plugin).open();
        }),
      );

    new Setting(containerEl)
      .setName(i18next.t("settings.publishByDefault.name"))
      .setDesc(i18next.t("settings.publishByDefault.description"))
      .addToggle((toggle) => {
        toggle.setValue(this.plugin.settings.publishByDefault).onChange((value) => {
          this.plugin.settings.publishByDefault = value;
          this.plugin.saveSettings();
        });
      });

    // 图片上传设置
    new Setting(containerEl)
      .setName(i18next.t("settings.imageUpload.enabled.name"))
      .setDesc(i18next.t("settings.imageUpload.enabled.description"))
      .addToggle((toggle) => {
        toggle.setValue(this.plugin.settings.imageUpload.enabled).onChange((value) => {
          this.plugin.settings.imageUpload.enabled = value;
          this.plugin.saveSettings();
        });
      });

    new Setting(containerEl)
      .setName(i18next.t("settings.imageUpload.uploadPath.name"))
      .setDesc(i18next.t("settings.imageUpload.uploadPath.description"))
      .addText((text) => {
        text.setValue(this.plugin.settings.imageUpload.uploadPath).onChange((value) => {
          this.plugin.settings.imageUpload.uploadPath = value;
          this.plugin.saveSettings();
        });
      });

    new Setting(containerEl)
      .setName(i18next.t("settings.imageUpload.preserveOriginal.name"))
      .setDesc(i18next.t("settings.imageUpload.preserveOriginal.description"))
      .addToggle((toggle) => {
        toggle.setValue(this.plugin.settings.imageUpload.preserveOriginal).onChange((value) => {
          this.plugin.settings.imageUpload.preserveOriginal = value;
          this.plugin.saveSettings();
        });
      });
  }
}
