// PRE-GENERATED (design session). Raw APO commands: writes into the APO config dir if present
// (so config.txt can Include it), else a preview file.
using System;
using System.IO;
using System.Windows;
using DansoriEQ.App.Apo;

namespace DansoriEQ.App;

public partial class CommandWindow : Wpf.Ui.Controls.FluentWindow
{
    private readonly string _path;
    private readonly bool _live;

    public CommandWindow()
    {
        InitializeComponent();
        var apoDir = ApoWriterFactory.ResolveConfigDir();
        _live = apoDir != null;
        _path = Path.Combine(apoDir ?? AppPaths.Root, "custom_apo.txt");
        if (File.Exists(_path)) Editor.Text = File.ReadAllText(_path);
    }

    private void Apply_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            File.WriteAllText(_path, Editor.Text);
            TxtStatus.Text = _live
                ? $"저장됨: {_path} · config.txt에 'Include: custom_apo.txt' 한 줄이 필요합니다."
                : $"저장됨(미리보기): {_path} · APO 설치 후 config로 연결됩니다.";
        }
        catch (Exception ex)
        {
            TxtStatus.Text = "실패: " + ex.Message + " (권한 문제일 수 있습니다)";
        }
    }

    private void Close_Click(object sender, RoutedEventArgs e) => Close();
}
