// Copyright (C) 2015-2026 The Neo Project.
//
// Helper.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 Akka.Actor;
using Neo.Network.P2P.Payloads;
using Neo.Properties;
using Neo.SmartContract;
using static Neo.Program;

namespace Neo.GUI;

internal static class Helper
{
    private static readonly Dictionary<Type, Form> tool_forms = new Dictionary<Type, Form>();

    private static void Helper_FormClosing(object sender, FormClosingEventArgs e)
    {
        tool_forms.Remove(sender.GetType());
    }

    public static void Show<T>() where T : Form, new()
    {
        Type t = typeof(T);
        if (!tool_forms.ContainsKey(t))
        {
            tool_forms.Add(t, new T());
            tool_forms[t].FormClosing += Helper_FormClosing;
        }
        tool_forms[t].Show();
        tool_forms[t].Activate();
    }

    public static void SignAndShowInformation(Transaction tx)
    {
        if (tx == null)
        {
            MessageBox.Show(Strings.InsufficientFunds);
            return;
        }
        ContractParametersContext context;
        try
        {
            context = new ContractParametersContext(Service.NeoSystem.StoreView, tx, Program.Service.NeoSystem.Settings.Network);
        }
        catch (InvalidOperationException)
        {
            MessageBox.Show(Strings.UnsynchronizedBlock);
            return;
        }
        Service.CurrentWallet.Sign(context);
        if (context.Completed)
        {
            tx.Witnesses = context.GetWitnesses();
            Service.NeoSystem.Blockchain.Tell(tx);
            InformationBox.Show(tx.Hash.ToString(), Strings.SendTxSucceedMessage, Strings.SendTxSucceedTitle);
        }
        else
        {
            InformationBox.Show(context.ToString(), Strings.IncompletedSignatureMessage, Strings.IncompletedSignatureTitle);
        }
    }
}
