package com.howard.rockear.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.howard.rockear.R;
import com.howard.rockear.Utils.ImageProvider;
import com.howard.rockear.service.RockEarService;
import com.howard.rockear.views.SmartBar;
import com.rockear.lrc.LrcReader;
import com.rockear.lrc.LyricView;

/**
 * @author howard smart bar as playing page and lrc display
 * 
 */
public class FragmentSmartBarHolder extends Fragment {

	private View mRootView;
	private SmartBar mDampView;
	private ImageView mDampImage;
	private TextView mTextView;
	private Context mContext;

	// lrcView
	private LyricView lrcView;
	private String currentLrcUrl = "";

	// typeface
	private Typeface typeface;

	// lrc handler
	private Handler lrcHandler = new Handler();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		mContext = activity.getApplicationContext();
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.fragment_smart_bar, null);

		initDampView();
		initTextView();
		initFontType();
		// initLrcView();

		return mRootView;
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onResume() {
		updateImageView();
		updateTextView();
		super.onResume();
	}

	private void initDampView() {
		mDampView = (SmartBar) mRootView.findViewById(R.id.dampview);
		mDampImage = (ImageView) mRootView.findViewById(R.id.damp_img);
		mDampView.setImageView(mDampImage);
	}

	private void initTextView() {

		mTextView = (TextView) mRootView.findViewById(R.id.tv_playing_main);
		mTextView.setTypeface(typeface);
	}

	private void initLrcView() {
		// lrcView = (LyricView) mRootView.findViewById(R.id.lrc_view);
		getLcrUrl();
		lrcHandler.post(new Runnable() {

			@Override
			public void run() {
				lrcView.setOffsetY(lrcView.getOffsetY() - lrcView.SpeedLrc()
						+ 8);
				// test
				lrcView.SelectIndex(100);
				lrcView.invalidate();
				lrcHandler.postDelayed(this, 100);
			}
		});
	}

	private void getLcrUrl() {
		currentLrcUrl = Environment.getExternalStorageDirectory().getPath()
				+ "/.rockEar/lrc/" + RockEarService.NOW_PLAYING_TITLE + ".lrc";
		LrcReader.read(currentLrcUrl);
		lrcView.SetTextSize();
		lrcView.setOffsetY(350);
	}

	/**
	 * update the bg
	 */
	public void updateImageView() {
		if (mDampImage != null) {
			mDampImage.setImageBitmap(ImageProvider.getArtWork(
					mContext,
					RockEarService.NOW_PLAYING_LIST.get(
							RockEarService.NOW_PLAYING_INDEX).getId(),
					RockEarService.NOW_PLAYING_LIST.get(
							RockEarService.NOW_PLAYING_INDEX).getAlbumId(),
					true));
		}
	}

	public void updateTextView() {
		if (mTextView != null) {
			mTextView.setTypeface(typeface);
			mTextView.setText(RockEarService.NOW_PLAYING_TITLE);
		}
	}

	private void initFontType() {
		typeface = Typeface.createFromAsset(mContext.getAssets(),
				"fonts/ruachlet.TTF");
	}
}
