package de.tudarmstadt.informatik.hostage.ui2.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
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 com.fortysevendeg.android.swipelistview.BaseSwipeListViewListener;

import java.util.LinkedList;
import java.util.List;

import de.tudarmstadt.informatik.hostage.Hostage;
import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
import de.tudarmstadt.informatik.hostage.model.Profile;
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
import de.tudarmstadt.informatik.hostage.ui2.activity.ProfileEditActivity;
import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
import de.tudarmstadt.informatik.hostage.ui2.swipelist.SwipeListView;

/**
 * @author Alexander Brakowski
 * @created 14.01.14 15:05
 */
public class ProfileManagerFragment extends Fragment {
	private ProfileManagerListAdapter mAdapter;

	private SharedPreferences mSharedPreferences;

	public ProfileManagerFragment(){}

	private SwipeListView list;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

	    super.onCreateView(inflater, container, savedInstanceState);
	    getActivity().setTitle(getResources().getString(R.string.drawer_profile_manager));
	    setHasOptionsMenu(true);

        View rootView = inflater.inflate(R.layout.fragment_profile_manager, container, false);
	    list = (SwipeListView) rootView.findViewById(R.id.profile_manager_listview);

		final ProfileManager pmanager = ProfileManager.getInstance();
		pmanager.loadData();

	    String sharedPreferencePath = MainActivity.getContext().getString(R.string.shared_preference_path);
	    mSharedPreferences = MainActivity.getContext().getSharedPreferences(sharedPreferencePath, Hostage.MODE_PRIVATE);

        final List<Profile> strList = new LinkedList<Profile>(pmanager.getProfilesList());

	    if(strList.size() > 0 && !mSharedPreferences.getBoolean("dismissedProfileSwipeHelp", false)){
		    Profile tProfile = new Profile();
		    tProfile.mShowTooltip = true;

			strList.add(1, tProfile);
	    }

		mAdapter = new ProfileManagerListAdapter(getActivity(), strList, list);
		pmanager.setProfileListAdapter(mAdapter);

        list.setAdapter(mAdapter);

		list.setSwipeListViewListener(new BaseSwipeListViewListener() {
			@Override
			public void onOpened(int position, boolean toRight){
				Profile profile = mAdapter.getItem(position);
				if(profile.mShowTooltip){
					mAdapter.remove(profile);
					strList.remove(profile);
					list.dismiss(position);

					mSharedPreferences.edit().putBoolean("dismissedProfileSwipeHelp", true).commit();
				}
			}

			@Override
			public void onClickFrontView(int position) {
				Profile profile = mAdapter.getItem(position);
				if(profile.mShowTooltip) return;

				pmanager.activateProfile(profile);

				mAdapter.notifyDataSetChanged();
			}
		});

        return rootView;
    }

	@Override
	public void onResume() {
		super.onResume();
		list.closeOpenedItems();
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// Inflate the menu items for use in the action bar
		inflater.inflate(R.menu.profile_manager_actions, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case R.id.profile_manager_action_add:
				Intent intent = new Intent(getActivity(), ProfileEditActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				getActivity().startActivity(intent);
				return true;
		}

		return false;
	}
}