package ru.nsu.bobrofon.easysshfs.mountpointList;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;

import ru.nsu.bobrofon.easysshfs.DrawerStatus;
import ru.nsu.bobrofon.easysshfs.EasySSHFSActivity;
import ru.nsu.bobrofon.easysshfs.R;

import ru.nsu.bobrofon.easysshfs.mountpointList.mountpoint.MountPoint;
import ru.nsu.bobrofon.easysshfs.mountpointList.mountpoint.MountPointsArrayAdapter;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Large screen devices (such as tablets) are supported by replacing the ListView
 * with a GridView.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
 * interface.
 */
public class MountpointFragment extends Fragment
	implements AbsListView.OnItemClickListener, MountPoint.Observer {

	private OnFragmentInteractionListener mListener;

	/**
	 * The fragment's ListView/GridView.
	 */
	private AbsListView mListView;

	/**
	 * The Adapter which will be used to populate the ListView/GridView with
	 * Views.
	 */
	private ListAdapter mAdapter;

	private DrawerStatus mDrawerStatus;

	private MountPointsList mountpoints;

	public void setDrawerStatus(final DrawerStatus drawerStatus) {
		mDrawerStatus = drawerStatus;
	}

	/**
	 * Mandatory empty constructor for the fragment manager to instantiate the
	 * fragment (e.g. upon screen orientation changes).
	 */
	public MountpointFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mountpoints	= MountPointsList.getIntent(getActivity());
		mAdapter = new MountPointsArrayAdapter(getActivity(), mountpoints.getMountPoints());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	                         Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		View view = inflater.inflate(R.layout.fragment_mountpoint, container, false);

		// Set the adapter
		mListView = (AbsListView) view.findViewById(android.R.id.list);
		((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

		// Set OnItemClickListener so we can be notified on item clicks
		mListView.setOnItemClickListener(this);

		mountpoints.registerObserver(this, getContext());
		// mountpoints.autoMount();

		return view;
	}

	@Override
	public void onDestroyView() {
		mountpoints.unregisterObserver(this);
		super.onDestroyView();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		((EasySSHFSActivity) activity).onSectionAttached(R.string.mount_point_list_title);
		try {
			mListener = (OnFragmentInteractionListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
				+ " must implement OnFragmentInteractionListener");
		}
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mListener = null;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (null != mListener) {
			// Notify the active callbacks interface (the activity, if the
			// fragment is attached to one) that an item has been selected.
			mListener.onFragmentInteraction(position);
		}
	}

	/**
	 * The default content for this Fragment has a TextView that is shown when
	 * the list is empty. If you would like to change the text, call this method
	 * to supply the text it should use.
	 */
	public void setEmptyText(CharSequence emptyText) {
		View emptyView = mListView.getEmptyView();

		if (emptyView instanceof TextView) {
			((TextView) emptyView).setText(emptyText);
		}
	}

	/**
	 * This interface must be implemented by activities that contain this
	 * fragment to allow an interaction in this fragment to be communicated
	 * to the activity and potentially other fragments contained in that
	 * activity.
	 * <p/>
	 * See the Android Training lesson <a href=
	 * "http://developer.android.com/training/basics/fragments/communicating.html"
	 * >Communicating with Other Fragments</a> for more information.
	 */
	public interface OnFragmentInteractionListener {
		void onFragmentInteraction(int id);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		if (mDrawerStatus == null || !mDrawerStatus.isDrawerOpen()) {
			inflater.inflate(R.menu.list, menu);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		if (id == R.id.action_new_mount_point) {
			mListener.onFragmentInteraction(mAdapter.getCount());
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public void onMountStateChanged(final MountPoint mountPoint) {
		mListView.invalidateViews();
	}

}

@@ -2293,22 +2293,8 @@
 ew =
- (AbsListView)
  vie
@@ -2334,36 +2334,8 @@
 ;%0A%09%09
-((AdapterView%3CListAdapter%3E) 
 mLis
@@ -2335,25 +2335,24 @@
 %0A%09%09mListView
-)
 .setAdapter(
@@ -3462,401 +3462,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * The default content for this Fragment has a TextView that is shown when%0A%09 * the list is empty. If you would like to change the text, call this method%0A%09 * to supply the text it should use.%0A%09 */%0A%09public void setEmptyText(CharSequence emptyText) %7B%0A%09%09View emptyView = mListView.getEmptyView();%0A%0A%09%09if (emptyView instanceof TextView) %7B%0A%09%09%09((TextView) emptyView).setText(emptyText);%0A%09%09%7D%0A%09%7D%0A%0A
 %09/**

fix mount fragment warnings