/*
 * =================================================================================================
 *                             Copyright (C) 2014 Martin Albedinsky
 * =================================================================================================
 *         Licensed under the Apache License, Version 2.0 or later (further "License" only).
 * -------------------------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy of this License 
 * you may obtain at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written within this file but as it 
 * is described in the License, the software distributed under the License is distributed on an 
 * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations under the License.
 * =================================================================================================
 */
package com.albedinsky.android.fragment.examples.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.MenuItemCompat;
import android.view.Menu;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.albedinsky.android.fragment.examples.R;
import com.albedinsky.android.fragment.manage.FragmentTransition;
import com.albedinsky.android.ui.widget.adapter.SimpleSpinnerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Martin Albedinsky
 */
public final class TransitionsAdapter extends SimpleSpinnerAdapter<FragmentTransition, TextView, TextView> {

	@SuppressWarnings("unused")
	private static final String TAG = "TransitionsAdapter";

	public TransitionsAdapter(Context context) {
		super(context);
		final List<FragmentTransition> items = new ArrayList<>(14);
		items.add(FragmentTransition.NONE);
		items.add(FragmentTransition.CROSS_FADE);
		items.add(FragmentTransition.SLIDE_TO_LEFT);
		items.add(FragmentTransition.SLIDE_TO_RIGHT);
		items.add(FragmentTransition.SLIDE_TO_BOTTOM);
		items.add(FragmentTransition.SLIDE_TO_TOP);
		items.add(FragmentTransition.SLIDE_TO_LEFT_AND_SCALE_OUT);
		items.add(FragmentTransition.SLIDE_TO_RIGHT_AND_SCALE_OUT);
		items.add(FragmentTransition.SLIDE_TO_BOTTOM_AND_SCALE_OUT);
		items.add(FragmentTransition.SLIDE_TO_TOP_AND_SCALE_OUT);
		items.add(FragmentTransition.SCALE_IN_AND_SLIDE_TO_LEFT);
		items.add(FragmentTransition.SCALE_IN_AND_SLIDE_TO_RIGHT);
		items.add(FragmentTransition.SCALE_IN_AND_SLIDE_TO_BOTTOM);
		items.add(FragmentTransition.SCALE_IN_AND_SLIDE_TO_TOP);
		changeItems(items);
	}

	@NonNull
	@Override
	protected View onCreateView(@NonNull ViewGroup parent, int position) {
		return inflate(R.layout.item_spinner_transition, parent);
	}

	@Override
	protected void onBindViewHolder(@NonNull TextView view, int position) {
		super.onBindViewHolder(view, position);
		view.setPadding(0, 0, 0, 0);
	}

	@Override
	protected void onUpdateViewHolder(@NonNull TextView view, @NonNull FragmentTransition transition, int position) {
		view.setText(transition.name);
	}

	@SuppressWarnings("ConstantConditions")
	public Menu populateMenu(Menu menu) {
		final SubMenu subMenu = menu.addSubMenu(Menu.NONE, -1, 0, mResources.getString(R.string.transitions_menu_title));
		MenuItemCompat.setShowAsAction(
				subMenu.getItem().setIcon(R.drawable.ic_action_action_open_with),
				MenuItemCompat.SHOW_AS_ACTION_ALWAYS
		);

		// Fill sub-menu with items of this adapter.
		final List<FragmentTransition> transitions = getItems();
		for (int i = 0; i < transitions.size(); i++) {
			final FragmentTransition transition = transitions.get(i);
			subMenu.add(Menu.NONE, i, Menu.NONE, replaceUnderscoreInName(transition));
		}
		return menu;
	}

	private String replaceUnderscoreInName(FragmentTransition transition) {
		return transition.name.replace("_", " ");
	}
}
