package com.rockear.lrc;

import java.util.TreeMap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class LyricView extends View {
	private float mX;
	private float offsetY;
	private float touchY;
	private int lrcIndex = 0;
	private int wordsize = 0;
	private int diatance = 25;
	Paint paint = new Paint();
	Paint paintHL = new Paint();

	public LyricView(Context context) {
		super(context);
		init();
	}

	public LyricView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (LrcReader.haslrc) {
			paintHL.setTextSize(wordsize + 3);
			paint.setTextSize(wordsize);
			LrcBean temp = LrcReader.lrc_map.get(lrcIndex);
			try {
				canvas.drawText(temp.lrc, mX, offsetY + (wordsize + diatance)
						* lrcIndex, paintHL);
			} catch (Exception e) {

			}
			// draw the lines that before the current
			for (int i = lrcIndex - 1; i >= 0; i--) {
				temp = LrcReader.lrc_map.get(i);
				if (offsetY + (wordsize + diatance) * i < 0) {
					break;
				}
				try {
					canvas.drawText(temp.lrc, mX, offsetY
							+ (wordsize + diatance) * i, paint);
				} catch (Exception e) {

				}
			}
			// draw those after the current lines
			for (int i = lrcIndex + 1; i < LrcReader.lrc_map.size(); i++) {
				temp = LrcReader.lrc_map.get(i);
				if (offsetY + (wordsize + diatance) * i > 1280) {
					break;
				}
				try {
					canvas.drawText(temp.lrc, mX, offsetY
							+ (wordsize + diatance) * i, paint);
				} catch (Exception e) {

				}
			}
		} else if (LrcConstant.SEARCHING) {
			paint.setTextSize(32);
			canvas.drawText("searching...", mX, 400, paint);
		} else if (LrcConstant.DOWNLOADING) {
			paint.setTextSize(32);
			canvas.drawText("downloading...", mX, 400, paint);
		} else {
			paint.setTextSize(32);
			canvas.drawText("find lrc", mX, 400, paint);
		}
		super.onDraw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float tt = event.getY();
		if (!LrcReader.haslrc) {
			return super.onTouchEvent(event);
		}
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			touchY = tt - touchY;
			offsetY = offsetY + touchY;
			break;
		case MotionEvent.ACTION_UP:
			break;
		}
		touchY = tt;
		return true;
	}

	public void init() {
		LrcReader.lrc_map = new TreeMap<Integer, LrcBean>();
		offsetY = 440;
		paint = new Paint();
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setColor(Color.parseColor("#b7b7b7"));
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setAntiAlias(true);
		paint.setDither(true);
		paint.setAlpha(180);

		paintHL = new Paint();
		paintHL.setTextAlign(Paint.Align.CENTER);
		paintHL.setColor(Color.parseColor("#ffffff"));
		paintHL.setTypeface(Typeface.DEFAULT_BOLD);
		paintHL.setAntiAlias(true);
		paintHL.setAlpha(255);
	}

	public void SetTextSize() {
		if (!LrcReader.haslrc) {
			return;
		}
		wordsize = 32;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mX = w * 0.5f;
		super.onSizeChanged(w, h, oldw, oldh);
	}

	/**
	 * get the speed
	 */
	public Float SpeedLrc() {
		float speed = 0;
		if (offsetY + (wordsize + diatance) * lrcIndex > 220) {
			speed = (offsetY + (wordsize + diatance) * lrcIndex - 220) / 20;
		} else if (offsetY + (wordsize + diatance) * lrcIndex < 220) {
			speed = (offsetY + (wordsize + diatance) * lrcIndex - 220) / 20;
		}
		return speed;
	}

	/**
	 * get the current line by the current time
	 * 
	 * @return current lrc index
	 */
	public int SelectIndex(int time) {
		if (!LrcReader.haslrc) {
			return 0;
		}
		int index = 0;
		for (int i = 0; i < LrcReader.lrc_map.size(); i++) {
			LrcBean temp = LrcReader.lrc_map.get(i);
			if (temp.begintime < time) {
				++index;
			}
		}
		lrcIndex = index - 1;
		if (lrcIndex < 0) {
			lrcIndex = 0;
		}
		return lrcIndex;
	}

	public float getOffsetY() {
		return offsetY;
	}

	public void setOffsetY(float offsetY) {
		this.offsetY = offsetY;
	}

	public int getSIZEWORD() {
		return wordsize;
	}

	public void setSIZEWORD(int sIZEWORD) {
		wordsize = sIZEWORD;
	}

}
