package library.core.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import library.core.model.CoreTextModel;
import library.core.model.CoreTextModel.TEXT_TYPE;

public class CoreTextView {

	private CoreTextModel model;
	
	public CoreTextView(CoreTextModel model){
		this.model=model;
	}
	
	public void draw(Canvas canvas){
		Paint p=new Paint();
		p.setAntiAlias(true);
		p.setColor(Color.BLACK);
		p.setTextSize(model.getSize());

		FontMetrics fontMetrics=p.getFontMetrics();

		float textW=p.measureText(model.getText());	//文字幅の取得

		float tx=0;
		float ty=0;
		float bx=0;
		float by=0;

		TEXT_TYPE type=model.getType();

		if(type==TEXT_TYPE.CENTER){
			p.setTextAlign(Align.CENTER);
		}else if(type==TEXT_TYPE.LEFT){
			p.setTextAlign(Align.LEFT);
		}else if(type==TEXT_TYPE.RIGHT){
			p.setTextAlign(Align.RIGHT);
		}

		//テキスト背景用の計算
		tx=model.getCenterX()-textW*0.5f-5;
		ty=model.getCenterY()-(fontMetrics.bottom-fontMetrics.top)*0.65f-5;
		bx=model.getCenterX()+textW*0.5f+5;
		by=model.getCenterY()+(fontMetrics.bottom-fontMetrics.top)*0.35f+5;

		Paint bp=new Paint();
		bp.setAntiAlias(true);
		bp.setTextSize(model.getSize());
		bp.setColor(Color.GRAY);
		bp.setTextAlign(Align.CENTER);
		RectF r=new RectF(tx+4,ty+4,bx+4,by+4);
		canvas.drawRoundRect(r, 5, 5, bp);

		bp.setColor(Color.LTGRAY);
		r=new RectF(tx,ty,bx,by);
		canvas.drawRoundRect(r, 5, 5, bp);
		canvas.drawText(model.getText(), model.getCenterX(), model.getCenterY(),p);
	}
	
	
	public void setModel(CoreTextModel model){
		this.model=model;
	}
}
