using GraphicsManager.Objects;
using Luski.net;
using Luski.net.Structures.Public;

namespace Luski.GUI.MainScreen.UI.LuskiControls;

public class FileUpload : UserControl
{
    public long? FileID;
    private PublicServer? PublicServer;
    private MainServer? MainServer;
    private string Path = "";
    public SocketFile? PublicSF;

    private void INIT(string FilePath)
    {
        Path = FilePath;
        FileInfo FI = new(FilePath);
        string fst = "";
        ulong size = (ulong)FI.Length;
        if (size < 1000)
            fst = size + " bytes";
        else if (size < 1000000)
            fst = Math.Round(size / (double)1000, 2) + " KB";
        else if (size < 1000000000)
            fst = Math.Round(size / (double)1000000, 2) + " MB";
        else if (size < 1000000000000) fst = Math.Round(size / (double)1000000000, 2) + " GB";
        Label fileNameLabel, fileSizeLabel;
        base.Size = new(333.ScaleInt(), 66.ScaleInt());
        base.BackgroundColor = new(40, 40, 40, 255);
        Controls.Add(fileSizeLabel = new Label(Globals.DefaultFont) { Text = fst, Location = new(64, 39) });
        Controls.Add(fileNameLabel = new Label(Globals.DefaultFont)
        {
            Color = new(102 / (float)255, 227 / (float)255, 170 / (float)255, 1), Text = FI.Name,
            Location = new(64, 6)
        });
        if (fileSizeLabel.Size.X > fileNameLabel.Size.X)
            base.Size = new(fileSizeLabel.Location.X + fileSizeLabel.Size.X + 5.ScaleInt(), base.Size.Y);
        else
            base.Size = new(fileNameLabel.Location.X + fileNameLabel.Size.X + 5.ScaleInt(), base.Size.Y);
    }
    
    public FileUpload(PublicServer ps, string FilePath)
    {
        INIT(FilePath);
        PublicServer = ps;
    }
    
    public FileUpload(MainServer ms, string FilePath)
    {
        INIT(FilePath);
        MainServer = ms;
    }

    public async Task StartUpload()
    {
        if (FileID is not null) return;
        if (PublicServer is not null)
        {
            PublicSF = await PublicServer.UploadFile(Path);
            return;
        }
    }
}