package fishrungames.bashgid;


import fishrungames.bashgid.core.HtmlDownloadManager.TextFileRecord;

import fishrungames.networkutils.ImageManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LinkContentFragment extends Fragment {

	
	
	public TextFileRecord contentRecord;
	
	//String content;
	
	View rootView;
	TextView titleTextView;
	ImageView imageView;
	TextView textView;
	Button button;
	
	ListView listView;
	
	ListAdapter listAdapter;
	
	public LinkContentFragment(TextFileRecord contentRecord)
	{
		this.contentRecord = contentRecord;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_link_content_page, container, false);
		
		
		listView = (ListView) rootView.findViewById(R.id.listView);
		
		listAdapter = new ListAdapter(getActivity());
		
		listView.setAdapter(listAdapter);
		
		//content = FileFunctions.ReadTextFromFile(contentRecord.localUrl + ".txt");
				
		return rootView;
	}
	
	@Override
    public void onResume()
    {
    	super.onResume();
    	
    }
	
	public class ListAdapter extends BaseAdapter {
	    private Context mContext;

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

	    public int getCount() {
	        return contentRecord.contentArr.size();
	    }

	    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
	    @SuppressLint("InflateParams")
	    public View getView(final 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.link_content_page_list_element, null);
	        	
	   
	        } else {
	        	view = convertView;
	        }
	        
	        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
	        
	        Button button = (Button) view.findViewById(R.id.button);
	        
	        TextView textView = (TextView) view.findViewById(R.id.textView);
	        
	        imageView.setVisibility(View.GONE);
	        button.setVisibility(View.GONE);
	        textView.setVisibility(View.GONE);
	        
	        if (contentRecord.contentArr.get(position).docPartType == TextFileRecord.DocPart.DocPartType.DPT_TEXT)
	        {
	        	textView.setVisibility(View.VISIBLE);
	        	textView.setText(contentRecord.contentArr.get(position).value);
	        	textView.setTextSize(20);
	        }
	        else if (contentRecord.contentArr.get(position).docPartType == TextFileRecord.DocPart.DocPartType.DPT_IMAGE)
	        {
	        	imageView.setVisibility(View.VISIBLE);
	        	ImageManager.getInstance().ApplyImageToImageView(imageView, contentRecord.contentArr.get(position).value);
	        }
	        else if (contentRecord.contentArr.get(position).docPartType == TextFileRecord.DocPart.DocPartType.DPT_BUTTON)
	        {
	        	button.setVisibility(View.VISIBLE);
	        	button.setText(R.string.go_to_website);
	        	button.setOnClickListener(new OnClickListener()
	    		{
	    			@Override
	    			public void onClick(View v)
	    			{
	    				Uri uri = Uri.parse(contentRecord.contentArr.get(position).value);
	    				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
	    				startActivity(intent);
	    			}
	    			
	    		});
	        }
	        
	        /*
	        ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
	        
	        Button button = (Button) view.findViewById(R.id.button);
	        
	        TextView textView = (TextView) view.findViewById(R.id.textView);
	        
	        ImageManager.getInstance().ApplyImageToImageView(imageButton, newsRecordArr.get(position).imageId);
	        
	        button.setText(newsRecordArr.get(position).title);
	        
	        textView.setText(NewsDataSource.iso8601Format.format(newsRecordArr.get(position).pubDate));
	        
	        button.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
	        
	        imageButton.setOnClickListener(new ButtonOnClickListener(newsRecordArr.get(position)));
	        */
	        return view;
	    }

	}
}
