// Copyright (C) 2015-2026 The Neo Project.
//
// TxOutListBox.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.Wallets;
using System.ComponentModel;

namespace Neo.GUI;

[DefaultEvent(nameof(ItemsChanged))]
internal partial class TxOutListBox : UserControl
{
    public event EventHandler ItemsChanged;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public AssetDescriptor Asset { get; set; }

    public int ItemCount => listBox1.Items.Count;

    public IEnumerable<TxOutListBoxItem> Items => listBox1.Items.OfType<TxOutListBoxItem>();

    [DefaultValue(false)]
    public bool ReadOnly
    {
        get
        {
            return !panel1.Enabled;
        }
        set
        {
            panel1.Enabled = !value;
        }
    }

    [DefaultValue(null)]
    public UInt160 ScriptHash
    {
        get;
        set
        {
            field = value;
            button3.Enabled = value == null;
        }
    }

    public TxOutListBox()
    {
        InitializeComponent();
    }

    public void Clear()
    {
        if (listBox1.Items.Count > 0)
        {
            listBox1.Items.Clear();
            button2.Enabled = false;
            ItemsChanged?.Invoke(this, EventArgs.Empty);
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        button2.Enabled = listBox1.SelectedIndices.Count > 0;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using PayToDialog dialog = new PayToDialog(asset: Asset, scriptHash: ScriptHash);
        if (dialog.ShowDialog() != DialogResult.OK) return;
        listBox1.Items.Add(dialog.GetOutput());
        ItemsChanged?.Invoke(this, EventArgs.Empty);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        while (listBox1.SelectedIndices.Count > 0)
        {
            listBox1.Items.RemoveAt(listBox1.SelectedIndices[0]);
        }
        ItemsChanged?.Invoke(this, EventArgs.Empty);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        using BulkPayDialog dialog = new BulkPayDialog(Asset);
        if (dialog.ShowDialog() != DialogResult.OK) return;
        listBox1.Items.AddRange(dialog.GetOutputs());
        ItemsChanged?.Invoke(this, EventArgs.Empty);
    }
}
