package com.howard.rockear.activity;

import java.util.ArrayList;
import java.util.Collections;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.howard.rockear.R;
import com.howard.rockear.Utils.MediaUtils;
import com.howard.rockear.adapters.PinyinComparator;
import com.howard.rockear.adapters.TrackListContent;
import com.howard.rockear.constant.AppConstant;

/**
 * @author howard splash and do some mess work background
 * 
 */
public class ActivityWelcome extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_welcome);

		initFontType();

		if (AppConstant.ALLTRACK_LIST != null
				&& AppConstant.ARTIST_CHILD_LIST != null
				&& AppConstant.ARTIST_GROUP_LIST != null) {
			new CountDownTimer(1000, 1000) {

				@Override
				public void onTick(long arg0) {

				}

				@Override
				public void onFinish() {
					Intent intent = new Intent(ActivityWelcome.this,
							ActivityRockEar.class);
					startActivity(intent);
					overridePendingTransition(R.anim.top_in_shake,
							R.anim.top_in_shake);
					ActivityWelcome.this.finish();
				}
			}.start();

		} else {
			new MySyncTask(this).execute("please do it...");
		}

	}

	private void initFontType() {
		Typeface typeFace = Typeface.createFromAsset(getAssets(),
				"fonts/ruachlet.TTF");
		TextView splash = (TextView) findViewById(R.id.splash_text);
		splash.setTypeface(typeFace);
		splash.setText("Melody Ear");
	}

	final class MySyncTask extends
			AsyncTask<String, String, ArrayList<TrackListContent>> {

		private Context mContext;
		private ArrayList<TrackListContent> allTracksList;

		private ArrayList<TrackListContent> groupList = null;
		private ArrayList<ArrayList<TrackListContent>> childList = null;

		/**
		 * @param mContext
		 */
		public MySyncTask(Context mContext) {
			super();
			this.mContext = mContext;
		}

		@Override
		protected ArrayList<TrackListContent> doInBackground(String... params) {

			allTracksList = new ArrayList<TrackListContent>();
			allTracksList = MediaUtils.GetMusicDataList(mContext);
			Collections.sort(allTracksList, new PinyinComparator());

			groupList = new ArrayList<TrackListContent>();
			childList = new ArrayList<ArrayList<TrackListContent>>();
			groupList = MediaUtils.findAllArtistFromList(MediaUtils
					.GetMusicDataList(mContext));
			// sort the group list first
			Collections.sort(groupList, new PinyinComparator());
			childList = MediaUtils.findTracksByArtist(mContext, groupList);

			AppConstant.ALLTRACK_LIST = allTracksList;
			AppConstant.ARTIST_GROUP_LIST = groupList;
			AppConstant.ARTIST_CHILD_LIST = childList;

			return null;
		}

		@Override
		protected void onPostExecute(ArrayList<TrackListContent> result) {
			Intent intent = new Intent(ActivityWelcome.this,
					ActivityRockEar.class);
			startActivity(intent);
			overridePendingTransition(R.anim.top_in_shake, R.anim.top_in_shake);
			finish();
			super.onPostExecute(result);
		}

	}

}
