import { world } from '@minecraft/server';
import { ModalFormData } from '@minecraft/server-ui';
import { resetSessionStatsForAllPlayers } from './cosmetics.js';

// Load saved or default values
let holidaySpawnChance = world.getDynamicProperty('holidaySpawnChance') ?? 0.01;
let legendarySpawnChance = world.getDynamicProperty('legendarySpawnChance') ?? 0.01;
let chanceOfMonsterGettingAHat = world.getDynamicProperty('chanceOfMonsterGettingAHat') ?? 0.05;

function formatPercent(value) {
  return value % 1 === 0 ? `${value}%` : `${value.toFixed(2)}%`;
}

//export async function openDefaultToSlimForm(player) {
  //const form = new ModalFormData()
    //.title("Default or Slim Skin")
    //.dropdown('Are you using default or slim skin', ["Default", "Slim"]);

  //const response = await form.show(player);
 // if (response.canceled || response.formValues[0] === 0) {
    //player.sendMessage("§7Default.");
    //return;
 // }


  //slimSkinSet();
  //player.sendMessage("§aCosmetics have been adjusted to fit slim model.");
//}
// === Individual Config Forms ===

export async function openResetSessionStatsForm(player) {
  const form = new ModalFormData()
    .title("⚠️ Reset Session Stats")
    .dropdown('Are you sure you want to reset all player session stats?\nThis will Reset everything but the Day and Date cosmetics were unlocked', ["Cancel", "Yes — Reset Everything"]);

  const response = await form.show(player);
  if (response.canceled || response.formValues[0] === 0) {
    player.sendMessage("§7Session stat reset canceled.");
    return;
  }

  // ✅ Actually perform the reset
  resetSessionStatsForAllPlayers();
  player.sendMessage("§aAll player session stats have been reset.");
}

export async function openHolidayChanceForm(player) {
  const form = new ModalFormData()
    .title('Holiday Bag Drop Chance')
    .textField('0.01 = 1% — 1.00 = 100%', holidaySpawnChance.toFixed(2));

  const response = await form.show(player);
  if (response.canceled) return;

  let input = parseFloat(response.formValues[0]);
  if (isNaN(input) || input < 0 || input > 1) input = 0.01;

  holidaySpawnChance = input;
  world.setDynamicProperty('holidaySpawnChance', holidaySpawnChance);
  player.sendMessage(`§aHoliday Bag Drop Chance: §f${formatPercent(holidaySpawnChance * 100)}\u0025`);
}

export async function openLegendaryChanceForm(player) {
  const form = new ModalFormData()
    .title('Legendary Bag Drop Chance')
    .textField('0.01 = 1% — 1.00 = 100%', legendarySpawnChance.toFixed(2));

  const response = await form.show(player);
  if (response.canceled) return;

  let input = parseFloat(response.formValues[0]);
  if (isNaN(input) || input < 0 || input > 1) input = 0.01;

  legendarySpawnChance = input;
  world.setDynamicProperty('legendarySpawnChance', legendarySpawnChance);
  player.sendMessage(`§aLegendary Bag Drop Chance: §f${formatPercent(legendarySpawnChance * 100)}\u0025`);
}

export async function openHatSpawnChanceForm(player) {
  const form = new ModalFormData()
    .title('Mob Hat Spawn Chance')
    .textField('0.01 = 1% — 1.00 = 100%', chanceOfMonsterGettingAHat.toFixed(2));

  const response = await form.show(player);
  if (response.canceled) return;

  let input = parseFloat(response.formValues[0]);
  if (isNaN(input) || input < 0 || input > 1) input = 0.05;

  chanceOfMonsterGettingAHat = input;
  world.setDynamicProperty('chanceOfMonsterGettingAHat', chanceOfMonsterGettingAHat);
  player.sendMessage(`§aMob Hat Spawn Chance: §f${formatPercent(chanceOfMonsterGettingAHat * 100)}\u0025`);
}

// === Getters ===
export function getHolidaySpawnChance() {
  return holidaySpawnChance;
}

export function getLegendarySpawnChance() {
  return legendarySpawnChance;
}

export function getchanceOfMonsterGettingAHat() {
  return chanceOfMonsterGettingAHat;
}
