#include "lgi/common/Lgi.h"
#include "AddFtpFile.h"
#include "resdefs.h"
#include "FtpFile.h"

AddFtpFile::AddFtpFile(LViewI *p, char *ftp)
{
	SetParent(p);
	Base = new LUri(ftp);
	Thread = 0;
	Files = Log = 0;
	if (LoadFromResource(IDD_FTP_FILE))
	{
		MoveToCenter();
		if (GetViewById(IDC_FILES, Files) &&
			GetViewById(IDC_LOG, Log))
		{
			FtpThread *t = GetFtpThread();
			if (t)
			{
				FtpCmd *cmd = new FtpCmd(FtpList, this);
				if (cmd)
				{
					cmd->Watch = Log;
					cmd->Uri = NewStr(ftp);
					t->Post(cmd);
					// Thread = new FtpThread(ftp, Files, Log);
				}
			}
		}
	}
}

AddFtpFile::~AddFtpFile()
{
	Uris.DeleteArrays();
}

void AddFtpFile::OnCmdComplete(FtpCmd *Cmd)
{
	if (Cmd && Base)
	{
		for (auto &e : Cmd->Dir)
		{
			if (e->Name && !e->IsDir())
			{
				LUri fu(Cmd->Uri);
				char path[256];
				if (Base->sPath)
					sprintf(path, "%s/%s", Base->sPath.Get(), e->Name.Get());
				else
					sprintf(path, "/%s", e->Name.Get());
				fu.sPath = path;
				Files->Insert(new FtpFile(e, fu.ToString()));
			}
		}

		Cmd->Dir.DeleteObjects();
	}
}

int AddFtpFile::OnNotify(LViewI *c, LNotification n)
{
	switch (c->GetId())
	{
		case IDOK:
		{
			List<LListItem> a;
			if (Files)
			{
				Files->GetAll(a);
				for (auto i: a)
				{
					FtpFile *f=dynamic_cast<FtpFile*>(i);
					if (!f) break;
					char *u = f->DetachUri();
					if (u)
						Uris.Add(u);
				}
			}
			EndModal(1);
			break;
		}
		case IDCANCEL:
		{
			EndModal(0);
			break;
		}
	}
	return 0;
}