import { App, PluginSettingTab, Setting } from 'obsidian';
import type BookPrintPlugin from '../main.js';

export class BookPrintSettingsTab extends PluginSettingTab {
  plugin: BookPrintPlugin;

  constructor(app: App, plugin: BookPrintPlugin) {
    super(app, plugin);
    this.plugin = plugin;
  }

  display(): void {
    const { containerEl } = this;
    containerEl.empty();

    containerEl.createEl('h2', { text: 'Book Print — Settings' });

    // Section 1: Default Book Information
    containerEl.createEl('h3', { text: 'Default Book Metadata' });

    new Setting(containerEl)
      .setName('Default Author Name')
      .setDesc('Default author name pre-populated in new Book Projects.')
      .addText((text) =>
        text
          .setPlaceholder('e.g. Joseph Rastovich')
          .setValue(this.plugin.pluginSettings.defaultAuthorName)
          .onChange(async (value) => {
            this.plugin.pluginSettings.defaultAuthorName = value;
            await this.plugin.saveSettings();
          })
      );

    new Setting(containerEl)
      .setName('Default Publisher / Imprint')
      .setDesc('Optional publishing imprint (e.g. Rastovich Press).')
      .addText((text) =>
        text
          .setPlaceholder('e.g. Rastovich Press')
          .setValue(this.plugin.pluginSettings.defaultPublisherImprint)
          .onChange(async (value) => {
            this.plugin.pluginSettings.defaultPublisherImprint = value;
            await this.plugin.saveSettings();
          })
      );

    new Setting(containerEl)
      .setName('Default Copyright Holder')
      .setDesc('Optional copyright holder name.')
      .addText((text) =>
        text
          .setPlaceholder('e.g. Joseph Rastovich')
          .setValue(this.plugin.pluginSettings.defaultCopyrightHolder)
          .onChange(async (value) => {
            this.plugin.pluginSettings.defaultCopyrightHolder = value;
            await this.plugin.saveSettings();
          })
      );

    new Setting(containerEl)
      .setName('Default Website')
      .setDesc('Optional author/publisher website printed on copyright page.')
      .addText((text) =>
        text
          .setPlaceholder('https://example.com')
          .setValue(this.plugin.pluginSettings.defaultWebsite)
          .onChange(async (value) => {
            this.plugin.pluginSettings.defaultWebsite = value;
            await this.plugin.saveSettings();
          })
      );

    // Section 2: Default Shipping Address
    containerEl.createEl('h3', { text: 'Default Shipping Address' });
    containerEl.createEl('p', {
      text: 'Saved locally for fast physical book checkout quotes. Never uploaded until an order is placed.',
      cls: 'setting-item-description',
    });

    const addr = this.plugin.pluginSettings.defaultShippingAddress || {
      fullName: '',
      addressLine1: '',
      addressLine2: '',
      city: '',
      state: '',
      postalCode: '',
      country: 'US',
      phone: '',
    };

    new Setting(containerEl)
      .setName('Full Name')
      .addText((t) =>
        t.setValue(addr.fullName).onChange(async (v) => {
          addr.fullName = v;
          this.plugin.pluginSettings.defaultShippingAddress = addr;
          await this.plugin.saveSettings();
        })
      );

    new Setting(containerEl)
      .setName('Address Line 1')
      .addText((t) =>
        t.setValue(addr.addressLine1).onChange(async (v) => {
          addr.addressLine1 = v;
          this.plugin.pluginSettings.defaultShippingAddress = addr;
          await this.plugin.saveSettings();
        })
      );

    new Setting(containerEl)
      .setName('City')
      .addText((t) =>
        t.setValue(addr.city).onChange(async (v) => {
          addr.city = v;
          this.plugin.pluginSettings.defaultShippingAddress = addr;
          await this.plugin.saveSettings();
        })
      );

    new Setting(containerEl)
      .setName('State / Province')
      .addText((t) =>
        t.setValue(addr.state).onChange(async (v) => {
          addr.state = v;
          this.plugin.pluginSettings.defaultShippingAddress = addr;
          await this.plugin.saveSettings();
        })
      );

    new Setting(containerEl)
      .setName('Postal Code')
      .addText((t) =>
        t.setValue(addr.postalCode).onChange(async (v) => {
          addr.postalCode = v;
          this.plugin.pluginSettings.defaultShippingAddress = addr;
          await this.plugin.saveSettings();
        })
      );

    new Setting(containerEl)
      .setName('Country (ISO 2-letter)')
      .addText((t) =>
        t.setValue(addr.country).onChange(async (v) => {
          addr.country = v;
          this.plugin.pluginSettings.defaultShippingAddress = addr;
          await this.plugin.saveSettings();
        })
      );

    // Section 3: Application & Backend API
    containerEl.createEl('h3', { text: 'Backend API Service' });

    new Setting(containerEl)
      .setName('Backend API URL')
      .setDesc('URL of the Book Print API server.')
      .addText((text) =>
        text
          .setPlaceholder('http://localhost:3000')
          .setValue(this.plugin.pluginSettings.backendApiUrl)
          .onChange(async (value) => {
            this.plugin.pluginSettings.backendApiUrl = value;
            await this.plugin.saveSettings();
          })
      );

    new Setting(containerEl)
      .setName('Print Environment')
      .setDesc('Select sandbox for testing or production for real orders.')
      .addDropdown((dropdown) =>
        dropdown
          .addOption('sandbox', 'Sandbox (Test Mode)')
          .addOption('production', 'Production (Real Printing)')
          .setValue(this.plugin.pluginSettings.printEnvironment)
          .onChange(async (value: string) => {
            this.plugin.pluginSettings.printEnvironment = value as 'sandbox' | 'production';
            await this.plugin.saveSettings();
          })
      );
  }
}
