#ifndef FILE_MANAGER_H
#define FILE_MANAGER_H

#include "pidir.h"
#include "piscreentiles.h"


class FileManager: public PIObject {
	PIOBJECT(FileManager)

public:
	FileManager();

	PIScreenTile * tile() const;
	PIScreenTile * localTile() const { return panels[0]; }
	PIScreenTile * remoteTile() const { return panels[1]; }

	enum Action {
		MkDir,
		Remove,
		Copy,
		Move,
		LocalCopy,
		Crypt,
		LocalCrypt
	};

	void setLocal();
	void setRemote();
	void setRemoteDir(const PIString & d);
	void setRemoteContent(const PIVector<PIFile::FileInfo> & el);
	PIString remoteDir() const { return panels[1]->dir.absolutePath(); }
	PIString localDir() const { return panels[0]->dir.absolutePath(); }
	int selectedPanel() const;
	PIStringList selectedRemote() const;
	PIFile::FileInfo currentRemoteEntry() const;
	PIFile::FileInfo selectedRemoteEntry(int index) const { return panels[1]->entries[index]; }
	void remoteSaveDir();
	void remoteRestoreDir();
	void readingRemote() const { panels[1]->showReading(); }
	void updateLocalDir() { panels[0]->updateDir(); }
	void updateRemoteDir() { panels[1]->updateDir(); }
	void clearSelectionLocal() { panels[0]->selected.clear(); }
	void clearSelectionRemote() { panels[1]->selected.clear(); }

	EVENT3(actionRequest, bool, remote_tile, FileManager::Action, type, PIVariant, data)

private:
	class TileDir: public TileList {
		PIOBJECT_SUBCLASS(TileDir, TileList)

	public:
		TileDir();
		void updateDir();
		void buildNames();
		bool keyEvent(PIKbdListener::KeyEvent key) override;
		void sizeHint(int & w, int & h) const override;
		void resizeEvent(int w, int h) override;
		void lock();
		void unlock();
		void showReading();
		void setContent(const PIVector<PIFile::FileInfo> & l);
		PIStringList selectedNames() const;
		TileSimple * label_path;
		PIVector<PIFile::FileInfo> entries;
		PIDir dir;
		PIMap<PIString, PIPair<int, int>> prev_pos;
		mutable PIMutex e_mutex;
		bool resized, is_right, remote_mode;
		void *fm, *key_func;
		EVENT3(actionRequest, bool, remote_tile, FileManager::Action, type, PIVariant, data)
	};

	EVENT1(tileKey, PIKbdListener::KeyEvent, key)
	static void tileKey_s(void * fm, PIKbdListener::KeyEvent key) { ((FileManager *)fm)->tileKey(key); }

	TileDir * panels[2];
	PIScreenTile * tile_root;
	typedef PIPair<PIString, PIString> SSPair;
};


#endif // FILE_MANAGER_H
