/*
 * ************************************************************************
 *
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2013 Adobe Systems Incorporated
 * All Rights Reserved.
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it.  If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe.
 *
 **************************************************************************
 */

package com.adobe.primetime.reference.ui.catalog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import android.support.v4.app.Fragment;
import com.adobe.primetime.reference.R;
import com.adobe.primetime.reference.PrimetimeReference;

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

public class ContentLoadingView extends Fragment {
	private final String LOG_TAG = PrimetimeReference.LOG_APP_NAME + "ContentLoadingView";

	private TextView txtMessage;
	private ProgressBar pbContentLoadingSpinner;
	private ImageButton btnContentRefresh;

	private List<RefreshContentEventListener> refreshContentEventListeners = new ArrayList<RefreshContentEventListener>();

	public interface RefreshContentEventListener {
		void onRefresh();
	}

	public void addRefreshContentEventListener(
			RefreshContentEventListener listener) {
		refreshContentEventListeners.add(listener);
	}

	public void removeRefreshContentEventListener(
			RefreshContentEventListener listener) {
		refreshContentEventListeners.remove(listener);
	}

	private void dispatchRefreshContentEvent() {
		for (RefreshContentEventListener listener : refreshContentEventListeners) {
			listener.onRefresh();
		}
	}

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

		txtMessage = (TextView) view
				.findViewById(R.id.txtCatalogContentLoading);
		pbContentLoadingSpinner = (ProgressBar) view
				.findViewById(R.id.pbCatalogContentLoading);
		btnContentRefresh = (ImageButton) view
				.findViewById(R.id.btnContentRefresh);
		btnContentRefresh.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				dispatchRefreshContentEvent();
			}
		});

		showLoadInProgress();

		return view;
	}

	public void showLoadInProgress() {
		PrimetimeReference.logger.i(LOG_TAG + "#showLoadInProgress",
				"Showing the load-in-progress indicator.");

		txtMessage.setText(getActivity().getResources().getString(
				R.string.LoadInProgress));
		pbContentLoadingSpinner.setVisibility(View.VISIBLE);
		btnContentRefresh.setVisibility(View.INVISIBLE);
	}

	public void showErrorMessage() {
		PrimetimeReference.logger.i(LOG_TAG + "#showErrorMessage",
				"Showing the error message.");

		txtMessage.setText(getActivity().getResources().getString(
				R.string.ErrorLoadingContent));
		pbContentLoadingSpinner.setVisibility(View.INVISIBLE);
		btnContentRefresh.setVisibility(View.VISIBLE);
	}
}
