import { world, system} from "@minecraft/server";
import { ModalFormData, ActionFormData} from "@minecraft/server-ui";
import { setOrbitCamera } from "./items";

const voice_type_list = [
    { name: "Male Knight", id: "male" },
    { name: "Male Barbarian", id: "male_barbaric"},
    { name: "Male Commoner", id: "male_commoner"},
    { name: "Female Knight", id: "female" },
    { name: "Female Barbarian", id: "female_barbaric"},
];

const camera_preset_type = [
    { name: "Vanilla", id: "vanilla"},
    { name: "Cinematic", id: "cinematic"},
    { name: "Dynamic First Person", id: "dynamic_df"}
]

const combo_audio_cue_list = [
  { name: "None", id: "none"},
  { name: "Banjo", id: "note.banjo"},
  { name: "Bass", id: "note.bass"},
  { name: "Bass Attack", id: "note.bassattack"},
  { name: "BD", id: "note.bd"},
  { name: "Bell", id: "note.bell"},
  { name: "Bit", id: "note.bit"},
  { name: "Chime", id: "note.chime"},
  { name: "Cow Bell", id: "note.cow_bell"},
  { name: "Didgeridoo", id: "note.didgeridoo"},
  { name: "Flute", id: "note.flute"},
  { name: "Guitar", id: "note.guitar"},
  { name: "Harp", id: "note.harp"},
  { name: "Hat", id: "note.hat"},
  { name: "Iron Xylophone", id: "note.iron_xylophone"},
  { name: "Pling", id: "note.pling"},
  { name: "Snare", id: "note.snare"},
  { name: "Xylophone", id: "note.xylophone"}
];

export function settingForm(player) {
    // Get current values or use defaults
    let voice_type = player.getDynamicProperty("voice_type") ?? "male";
    let voice_pitch = player.getDynamicProperty("voice_pitch") ?? 1;
    let voice_volume = player.getDynamicProperty("voice_volume") ?? 1;
    let attack_volume = player.getDynamicProperty("attack_volume") ?? 1;
    let parry_volume = player.getDynamicProperty("parry_volume") ?? 1;

    let camera_vertical = player.getDynamicProperty("camera_vertical") ?? 0;
    let camera_horizontal = player.getDynamicProperty("camera_horizontal") ?? 0;
    let camera_forward = player.getDynamicProperty("camera_forward") ?? 0;
    let camera_pitch = player.getDynamicProperty("camera_pitch") ?? 15; // default pitch
    let camera_yaw = player.getDynamicProperty("camera_yaw") ?? 0;      // default yaw
    let camera_preset = player.getDynamicProperty("camera_preset") ?? "vanilla";
    let combo_audio_cue = player.getDynamicProperty("audio_cue") ?? "Bit";
    let cue_volume = player.getDynamicProperty("cue_volume") ?? 1;

    // Find the correct dropdown index for the stored id
    const defaultIndex = voice_type_list.findIndex(t => t.id === voice_type);
    const defaultCamIndex = camera_preset_type.findIndex(t => t.id === camera_preset);
    const defaultCueIndex = combo_audio_cue_list.findIndex(t => t.id === combo_audio_cue);
    // console.warn(defaultCamIndex)

    const modalForm = new ModalFormData().title('⚙ Settings');
    modalForm.dropdown(
        "Voice Type",
        voice_type_list.map((type) => type.name),
        { defaultValueIndex: defaultIndex === -1 ? 0 : defaultIndex }
    );
     modalForm.slider("Voice Pitch", 8, 12, { defaultValue: voice_pitch * 10, valueStep: 1 });
    modalForm.slider("Voice Volume", 0, 20, { defaultValue: voice_volume * 10, valueStep: 1 });
    modalForm.slider("Attack Volume", 0, 20, { defaultValue: attack_volume * 10, valueStep: 1 });
    modalForm.slider("Parry Volume", 0, 20, { defaultValue: parry_volume * 10, valueStep: 1 });

    modalForm.slider("Camera Vertical", -80, 80, { defaultValue: camera_vertical * 10, valueStep: 1 });
    modalForm.slider("Camera Horizontal", -80, 80, { defaultValue: camera_horizontal * 10, valueStep: 1 });
    modalForm.slider("Camera Forward", -80, 80, { defaultValue: camera_forward * 10, valueStep: 1 });

    modalForm.slider("Camera Pitch", -90, 90, { defaultValue: camera_pitch, valueStep: 1 });
    modalForm.slider("Camera Yaw", -180, 180, { defaultValue: camera_yaw, valueStep: 1 });

    modalForm.dropdown(
        "Camera Type",
        camera_preset_type.map((type) => type.name),
        { defaultValueIndex: defaultCamIndex === -1 ? 0 : defaultCamIndex }
    );

    modalForm.dropdown(
        "Combo Audio Cues",
        combo_audio_cue_list.map((type) => type.name),
        { defaultValueIndex: defaultCueIndex === -1 ? 0 : defaultCueIndex }
    );

    modalForm.slider("Combo Audio Cues Volume", 0, 20, { defaultValue: cue_volume * 10, valueStep: 1 });

    modalForm.show(player).then(formData => {
        if (formData.canceled) return;

        // Get new values
        const newVoiceTypeIndex = formData.formValues[0];
        const newVoiceTypeId = voice_type_list[newVoiceTypeIndex].id;
        const newVoicePitch = formData.formValues[1] / 10;
        const newVoiceVolume = formData.formValues[2] / 10;
        const newAttackVolume = formData.formValues[3] / 10;
        const newParryVolume = formData.formValues[4] / 10;
        const newCameraVertical = formData.formValues[5] / 10;
        const newCameraHorizontal = formData.formValues[6] / 10;
        const newCameraForward = formData.formValues[7] / 10;
        const newCameraPitch = formData.formValues[8]; // rotation x
        const newCameraYaw = formData.formValues[9];   // rotation y
        const newCameraPresetIndex = formData.formValues[10];
        const newCameraPresetId = camera_preset_type[newCameraPresetIndex].id;
        const newAudioCueIndex = formData.formValues[11];
        const newAudioCueId = combo_audio_cue_list[newAudioCueIndex].id;
        const newCueVolume = formData.formValues[12] / 10;

        player.sendMessage(`§l§6||--- Settings Updated ---||`);
        if (newVoiceTypeId !== voice_type) {
            player.setDynamicProperty("voice_type", newVoiceTypeId);
            player.sendMessage(`§aVoice Type set to: §e${voice_type_list[newVoiceTypeIndex].name}`);
        }

        if (newVoicePitch !== voice_pitch) {
            player.setDynamicProperty("voice_pitch", newVoicePitch);
            player.sendMessage(`§aVoice Pitch set to: §e${formData.formValues[1]}`);
        }

        if (newVoiceVolume !== voice_volume) {
            player.setDynamicProperty("voice_volume", newVoiceVolume);
            player.sendMessage(`§aVoice Volume set to: §e${formData.formValues[2]}`);
        }

        if (newAttackVolume !== attack_volume) {
            player.setDynamicProperty("attack_volume", newAttackVolume);
            player.sendMessage(`§aAttack Volume set to: §e${formData.formValues[3]}`);
        }

        if (newParryVolume !== parry_volume) {
            player.setDynamicProperty("parry_volume", newParryVolume);
            player.sendMessage(`§aParry Volume set to: §e${formData.formValues[4]}`);
        }

        if (newCameraVertical !== camera_vertical) {
            player.setDynamicProperty("camera_vertical", newCameraVertical);
            player.sendMessage(`§aCamera Vertical set to: §e${formData.formValues[5]}`);
        }

        if (newCameraHorizontal !== camera_horizontal) {
            player.setDynamicProperty("camera_horizontal", newCameraHorizontal);
            player.sendMessage(`§aCamera Horizontal set to: §e${formData.formValues[6]}`);
        }

        if (newCameraForward !== camera_forward) {
            player.setDynamicProperty("camera_forward", newCameraForward);
            player.sendMessage(`§aCamera Forward set to: §e${formData.formValues[7]}`);
        }

                // Set dynamic rotation properties
        if (newCameraPitch !== camera_pitch) {
            player.setDynamicProperty("camera_pitch", newCameraPitch);
            player.sendMessage(`§aCamera Pitch set to: §e${newCameraPitch}`);
        }

        if (newCameraYaw !== camera_yaw) {
            player.setDynamicProperty("camera_yaw", newCameraYaw);
            player.sendMessage(`§aCamera Yaw set to: §e${newCameraYaw}`);
        }

        if (newCameraPresetId !== camera_preset) {
            player.setDynamicProperty("camera_preset", newCameraPresetId);
            player.sendMessage(`§aCamera Type set to: §e${camera_preset_type[newCameraPresetIndex].name}`);
            if (newCameraPresetId === "vanilla"){
                player.runCommand("/camera @s clear");
                if (player.hasTag("ae_fpm.is_first_person")) {
                    player.runCommand("/ae_fpm:toggle_model")
                }
            } else if (newCameraPresetId === "dynamic_df") {
                player.runCommand("/ae_fpm:toggle_model")
                player.addTag("ae_fpm.disable_animation");
            } else {
                if (player.hasTag("ae_fpm.is_first_person")) {
                    player.runCommand("/ae_fpm:toggle_model")
                }
            }
        }

        if (newAudioCueId !== combo_audio_cue) {
            player.setDynamicProperty("audio_cue", newAudioCueId);
            player.sendMessage(`§aCombo Indicator Sound set to: §e${combo_audio_cue_list[newAudioCueIndex].name}`);
        }

        if (newCueVolume !== cue_volume) {
            player.setDynamicProperty("cue_volume", newCueVolume);
            player.sendMessage(`§aCombo Audio Cues Volume set to: §e${formData.formValues[12]}`);
        }
    });
}

