package fishrungames.bashgid;

import java.util.ArrayList;

import fishrungames.bashgid.core.ImageManager;
import fishrungames.bashgid.core.NewsManager;
import fishrungames.bashgid.core.NewsManager.NewsRecord;
import fishrungames.bashgid.core.NewsUpdatedCallbackInterface;
import fishrungames.bashgid.core.RemoveCallbackInterface;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import it.sephiroth.android.library.widget.HListView;

public class MainPageFragment extends Fragment implements NewsUpdatedCallbackInterface {
	
	
	String [] teaserHeaderArray = { "Для инвестора", "Культура Башкортостана", "Для гостей и туристов", "Образование" };
	
	int[] investorGroupContentIdArray = { 1, 2, 3, 4 };
	
	int[] cultureGroupContentIdArray = { 5, 6, 7 };
	
	int[] touristGroupContentIdArray = { 8, 9, 10, 11 };
	
	
	int[] educationGroupContentIdArray = { 12, 13, 14 };
	
	ListView listView;
	View header;
	
	RemoveCallbackInterface downloadNewsCanceller = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_main_page, container, false);
			
		listView = (ListView) rootView.findViewById(R.id.listView);
		
		listView.setAdapter(new ListAdapter(getActivity()));
		
		header = inflater.inflate(R.layout.main_page_list_header, null);
		
		RefreshHeader();
		
		downloadNewsCanceller = NewsManager.GetInstance().DownloadNews(this);
		
		listView.addHeaderView(header);
		
		Button moreNewsButton = (Button) header.findViewById(R.id.moreNewsButton);
		
		moreNewsButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				MainActivity.getInstance().OpenNewsScreen();
			}
		}
		);

		return rootView;
	}
	
    @Override
	public void onDestroyView()
	{
    	if (downloadNewsCanceller != null)
    	{
    		downloadNewsCanceller.RemoveCallback();
    		downloadNewsCanceller = null;
    	}
		super.onDestroyView();
	}
    
	
	
	private void RefreshHeader()
	{
		
		
		ImageButton news1ImageButton = (ImageButton) header.findViewById(R.id.news1ImageButton);
		ImageButton news2ImageButton = (ImageButton) header.findViewById(R.id.news2ImageButton);
		ImageButton news3ImageButton = (ImageButton) header.findViewById(R.id.news3ImageButton);
		
		Button news1Button = (Button)header.findViewById(R.id.news1Button);
		Button news2Button = (Button)header.findViewById(R.id.news2Button);
		Button news3Button = (Button)header.findViewById(R.id.news3Button);
		
		
		ArrayList<NewsManager.NewsRecord> newsRecordArr = NewsManager.GetInstance().getNews();
		
		if (newsRecordArr.size() > 0)
		{	
			ImageManager.getInstance().ApplyImageToImageView(news1ImageButton, newsRecordArr.get(0).imageId);
			news1Button.setText(newsRecordArr.get(0).title);
			
			news1ImageButton.setOnClickListener(new NewsButtonOnClickListener(newsRecordArr.get(0)));
			news1Button.setOnClickListener(new NewsButtonOnClickListener(newsRecordArr.get(0)));
		}
		else
		{
			news1ImageButton.setImageResource(R.drawable.transparent);
			news1Button.setText(R.string.news_record_not_available);
			news1ImageButton.setOnClickListener(null);
			news1Button.setOnClickListener(null);
		}
		
		if (newsRecordArr.size() > 1)
		{	
			ImageManager.getInstance().ApplyImageToImageView(news2ImageButton, newsRecordArr.get(1).imageId);
			news2Button.setText(newsRecordArr.get(1).title);
			news2ImageButton.setOnClickListener(new NewsButtonOnClickListener(newsRecordArr.get(1)));
			news2Button.setOnClickListener(new NewsButtonOnClickListener(newsRecordArr.get(1)));
		}
		else
		{
			news2ImageButton.setImageResource(R.drawable.transparent);
			news2Button.setText(R.string.news_record_not_available);
			news2ImageButton.setOnClickListener(null);
			news2Button.setOnClickListener(null);
		}
		
		if (newsRecordArr.size() > 2)
		{	
			ImageManager.getInstance().ApplyImageToImageView(news3ImageButton, newsRecordArr.get(2).imageId);
			news3Button.setText(newsRecordArr.get(2).title);
			news3ImageButton.setOnClickListener(new NewsButtonOnClickListener(newsRecordArr.get(2)));
			news3Button.setOnClickListener(new NewsButtonOnClickListener(newsRecordArr.get(2)));
		}
		else
		{
			news3ImageButton.setImageResource(R.drawable.transparent);
			news3Button.setText(R.string.news_record_not_available);
			news3ImageButton.setOnClickListener(null);
			news3Button.setOnClickListener(null);
		}
	
		
	}
	
	
	public class ListAdapter extends BaseAdapter {
	    private Context mContext;

	    public ListAdapter(Context c) {
	        mContext = c;
	    }

	    public int getCount() {
	        return teaserHeaderArray.length;
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	        
	    	
	    	View view;
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	        	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        	view = inflater.inflate(R.layout.main_page_list_element, null);
	        	
	   
	        } else {
	        	view = convertView;
	        }
	        
	        TextView textView = (TextView) view.findViewById(R.id.textView);
	        
	        Button button = (Button) view.findViewById(R.id.button);
	        
	        HListView hListView = (HListView) view.findViewById(R.id.hListView);
	        
	        if (position == 0)
	        {
	        
	        	hListView.setAdapter(new MainPageHListViewAdapters.SimpleElementAdapter(getActivity(), investorGroupContentIdArray));
	        }
	        else if (position == 1)
	        {
	        	hListView.setAdapter(new MainPageHListViewAdapters.SimpleElementAdapter(getActivity(), cultureGroupContentIdArray));
	        }
	        else if (position == 2)
	        {
	        	hListView.setAdapter(new MainPageHListViewAdapters.SimpleElementAdapter(getActivity(), touristGroupContentIdArray));
	        }
	        else
	        {
	        	hListView.setAdapter(new MainPageHListViewAdapters.SimpleElementAdapter(getActivity(), educationGroupContentIdArray));
	        }
	        
	        textView.setText(teaserHeaderArray[position]);
	        
	        button.setText(R.string.see_all);

	        return view;
	    }

	}


	@Override
	public void OnNewsUpdated()
	{
		getActivity().runOnUiThread(new Runnable() {

			@Override
			public void run()
			{
				RefreshHeader();
			}
			
		});
	}
	
	public static class NewsButtonOnClickListener implements OnClickListener
	{
		
		NewsRecord newsRecord;
		
		public NewsButtonOnClickListener(NewsRecord newsRecord)
		{
			this.newsRecord = newsRecord;
		}

		@Override
		public void onClick(View v)
		{
			MainActivity.getInstance().OpenNewsRecordScreen(newsRecord, MainActivity.TAG_FROM_MAINFRAGMENT_TO_NEWSRECORDFRAGMENT);
		}
		
	}
		
}
