package hh.dejsem.fm;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import hh.dejsem.K;
import hh.dejsem.Prefs;
import hh.dejsem.R;
import hh.lib.D;

/**
 * File Manager focused on both local and server FS maintained on 2-panel layout
 */
public class FragFMServer extends FragFM {

	public static FragFMServer instantiate(D d) { return (FragFMServer) FragFM.instantiate(d, new FragFMServer()); }

	PanelDirView actualDirPanel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		d.actMsgr = new Messenger(new Handler(this));   // zachytíme žádosti o refresh dir listu

		fmLayout = (LinearLayout)LayoutInflater.from(d.getContext()).inflate(R.layout.server_file_manager, null);
		try {
			String s;
			s = FragFM.getCachedCwdName(d, K.CACHED_LOC_CWD_KEY);
			String locCwdName = (s == null ? Prefs.homeDir : s);
			s = FragFM.getCachedCwdName(d, K.cacheFileR);
			String remCwdName = (s == null ? "" : s);
			String[] locSelection = new String[0];
			String[] srvSelection = new String[0];
			int locListPosition = 0;
			int srvListPosition = 0;
			if (savedInstanceState != null) {
				if(savedInstanceState.containsKey(K.FM_LOC_SAVED_SELECTION_KEY))
					locSelection = savedInstanceState.getStringArray(K.FM_LOC_SAVED_SELECTION_KEY);
				if(savedInstanceState.containsKey(K.FM_LOC_SAVED_POSITION_KEY))
					locListPosition = savedInstanceState.getInt(K.FM_LOC_SAVED_POSITION_KEY);
				if(savedInstanceState.containsKey(K.FM_SRV_SAVED_SELECTION_KEY))
					srvSelection = savedInstanceState.getStringArray(K.FM_SRV_SAVED_SELECTION_KEY);
				if(savedInstanceState.containsKey(K.FM_SRV_SAVED_POSITION_KEY))
					srvListPosition = savedInstanceState.getInt(K.FM_SRV_SAVED_POSITION_KEY);
			}
			remoteDirPanel = new PanelServerDir(d, (RelativeLayout)fmLayout.findViewById(R.id.remote), "server", remCwdName);
			remoteDirPanel.fm.setStrArrSelection(srvSelection);
			remoteDirPanel.fm.setCurrListPos(srvListPosition);

			localDirPanel = new PanelLocalDir(d, (RelativeLayout)fmLayout.findViewById(R.id.local), "local", locCwdName);
			localDirPanel.fm.setStrArrSelection(locSelection);
			localDirPanel.fm.setCurrListPos(locListPosition);

			remoteDirPanel.otherSidePanel = localDirPanel;
			localDirPanel.otherSidePanel = remoteDirPanel;

			if(d.ll(4)) d.l(String.format("created, remote cwd=%s, local cwd=%s",
					remoteDirPanel.fm.getCwdName(), localDirPanel.fm.getCwdName()));
		}
		catch(Exception e) { d.abendMsg("initiating dir lists", e); }
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if((Integer)v.getTag() == K.LOC_FS) actualDirPanel = localDirPanel;
		else actualDirPanel = remoteDirPanel;
		actualDirPanel.buildContextMenu(menu, v, menuInfo);
		setUserVisibleHint(true);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if(d.ll(4)) d.l(String.format("context menu item selected=%s", item.getTitle()));
		if(actualDirPanel.handleContextItemSelected(item)) return true;
		return super.onContextItemSelected(item);
	}

	@Override
	public void onSaveInstanceState(Bundle b) {
		super.onSaveInstanceState(b);
		b.putStringArray(K.FM_LOC_SAVED_SELECTION_KEY, localDirPanel.fm.getStrArrSelection());
		b.putInt(K.FM_LOC_SAVED_POSITION_KEY, localDirPanel.fm.getCurrListPos());
		b.putStringArray(K.FM_SRV_SAVED_SELECTION_KEY, remoteDirPanel.fm.getStrArrSelection());
		b.putInt(K.FM_SRV_SAVED_POSITION_KEY, remoteDirPanel.fm.getCurrListPos());
	}

	@Override
	public boolean handleMessage(Message msg) {
		if (d.ll(4)) d.l("handle message in FragFMServer, what=" + msg.what);
		switch(msg.what) {
			case K.MSG_REFRESH_LOC:
				localDirPanel.refreshDirList();
				return true;
			case K.MSG_REFRESH_SRV:
				remoteDirPanel.refreshDirList();
				return true;
		}
		return super.handleMessage(msg);
	}
}
