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;

/**
 * fragment for local file manager focused on PEER operations on one-panel layout
 */
public class FragFMPeer extends FragFM {

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

	@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.peer_file_manager, null);
		try {
			final String cachedCwdName = FragFM.getCachedCwdName(d, K.CACHED_LOC_CWD_KEY);
			final String locCwdName = (cachedCwdName != null ? cachedCwdName : Prefs.homeDir);
			String[] selection = new String[0];
			int listPosition = 0;
			if(savedInstanceState != null) {
				if(savedInstanceState.containsKey(K.FM_LOC_SAVED_SELECTION_KEY))
					selection = savedInstanceState.getStringArray(K.FM_LOC_SAVED_SELECTION_KEY);
				if(savedInstanceState.containsKey(K.FM_LOC_SAVED_POSITION_KEY))
					listPosition = savedInstanceState.getInt(K.FM_LOC_SAVED_POSITION_KEY);
			}
			localDirPanel = new PanelPeerDir(d, (RelativeLayout)fmLayout.findViewById(R.id.local), locCwdName);
			localDirPanel.fm.setStrArrSelection(selection);
			localDirPanel.fm.setCurrListPos(listPosition);
			if(d.ll(4)) d.l("created local cwd filelist " + 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);
		localDirPanel.buildContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if(d.ll(4)) d.l(String.format("context menu item selected=%s", item.getTitle()));
		if(localDirPanel.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());
	}

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

	/*@Override
	public void onDestroy() {
		super.onDestroy();
		NetUDP.go = false;  // potlačení případných UDP-broadcastů
	}*/
}
