package kimiko.coreen.app.view.Fragment;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import kimiko.coreen.app.R;
import kimiko.coreen.app.data.base.MotDAO;
import kimiko.coreen.app.data.ui.CopierListener;
import kimiko.coreen.app.data.ui.MotAdapter;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class ExempleFragment extends DetailFragment implements OnInitListener {
	private String tradText, hangText;
	private TextView hang, roma, trad;
	private ImageButton lecture;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_exemple, container, false);
		setHasOptionsMenu(true);
		this.tts = new TextToSpeech(this.getActivity(), this);
		this.tts.setSpeechRate(0.8f);

		Bundle b = this.getActivity().getIntent().getExtras();
		if(b==null)
			b = getArguments();

		this.tradText = b.getString("trad");
		this.hangText = b.getString("hang");

		this.hang = ((TextView)view.findViewById(R.id.hang));
		this.roma = ((TextView)view.findViewById(R.id.roma));
		this.trad = ((TextView)view.findViewById(R.id.trad));

		this.lecture = ((ImageButton)view.findViewById(R.id.lecture));
		this.lecture.setAlpha(0.5f);
		ListView listView = (ListView) view.findViewById(R.id.mots);

		this.hang.setText(b.getString("hang"));
		this.roma.setText(b.getString("rom"));
		this.trad.setText(b.getString("trad"));

		this.hang.setOnLongClickListener(new CopierListener(this.getActivity(), b.getString("hang")));
		this.roma.setOnLongClickListener(new CopierListener(this.getActivity(), b.getString("rom")));
		this.trad.setOnLongClickListener(new CopierListener(this.getActivity(), b.getString("trad")));



		MotDAO motDAO = new MotDAO(this.getActivity());

		try {
			motDAO.open();

			Set<String> listmot = new HashSet<String>(Arrays.asList(b.getString("hang").split(" ")));
			Set<String> listmotTMP = new HashSet<String>();
			Cursor particules = motDAO.chercherType("particule");
			while(particules.moveToNext())
			{
				String par = particules.getString(1);
				for(String lmot : listmot)
				{
					Log.v(this.getClass().getCanonicalName(), "-----");
					Log.v(this.getClass().getCanonicalName(), par);
					Log.v(this.getClass().getCanonicalName(), lmot);
					Log.v(this.getClass().getCanonicalName(), par.length()+"");
					Log.v(this.getClass().getCanonicalName(), lmot.length()+"");
					if(lmot.length() <= par.length())
						continue;
					String subS = lmot.substring(lmot.length()-par.length());
					if(subS.equals(par))
					{
						String subSS = lmot.substring(0, lmot.length()-par.length());
						listmotTMP.add(subSS);
						//listmotTMP.add(par);
					}
				}
			}
			listmot.addAll(listmotTMP);

			Cursor mots = motDAO.chercherHangeul(listmot);
			MotAdapter adapter = new MotAdapter(this.getActivity(), mots);
			listView.setAdapter(adapter);
			motDAO.close();
		} catch (SQLiteDatabaseLockedException ex) {
			ex.printStackTrace();
		}

		return view;
	}

	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = TextToSpeech.LANG_MISSING_DATA;
			if(Locale.getDefault().equals(Locale.KOREA)||Locale.getDefault().equals(Locale.KOREAN))
			{
				Locale loc = new Locale("fre", "FRA");
				result = tts.setLanguage(loc);
			}
			else
			{
				result = tts.setLanguage(Locale.KOREA);
			}
			if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
				this.ttsSupported=false;
			}
			this.lecture.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(!ttsSupported)
					{
						Log.e("TTS", "This Language is not supported");
						Intent installIntent = new Intent();
						installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
						startActivity(installIntent);
					}

					if(Locale.getDefault().equals(Locale.KOREA)||Locale.getDefault().equals(Locale.KOREAN))
						tts.speak(tradText, TextToSpeech.QUEUE_FLUSH, null);
					else
						tts.speak(hangText, TextToSpeech.QUEUE_FLUSH, null);
				}
			});
			this.lecture.setAlpha(1f);
		} else {
			Log.e("TTS", "Initilization Failed!");
		}
	}

	@Override
	public void onDestroy() {
		this.tts.shutdown();
		super.onDestroy();
	}

}
