package com.sabirjan.Kanun;
import com.sabirjan.Dialog.Custom_WaitDialog;
import com.sabirjan.android.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.widget.TextView;

public class TodoListItemView extends TextView {

    private Paint marginPaint;
    private Paint linePaint;
    private int paperColor;
    private float margin;
    Typeface face;
    String strText="";
    String []str;

    public TodoListItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    	 init(context);
    }

    public TodoListItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    public TodoListItemView(Context context) {
        super(context);

        init(context);
    }

    private void init(Context context) 
    {
        Resources myResources = getResources();
      	 face=Typeface.createFromAsset(this.getContext().getAssets(),"fonts/ALKATIPTorTom.TTF");//
        // Create the paint brushes we will use in the onDraw method.
        marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
        marginPaint.setTypeface(face);
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setColor(myResources.getColor(R.color.notepad_lines));
        linePaint.setTypeface(face);
        // Get the paper background color and the margin width.
        paperColor = myResources.getColor(R.color.notepad_paper);
        margin = myResources.getDimension(R.dimen.notepad_margin);
       
   
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
    	
    	
        // TODO Auto-generated method stub
        // Color as paper
        canvas.drawColor(paperColor);
      

        // Draw ruled lines
        canvas.drawLine(0, 0, getMeasuredWidth(), 0, linePaint);
        canvas.drawLine(0, getMeasuredHeight(), 
                           getMeasuredWidth(), getMeasuredHeight(), 
                           linePaint);
        
        // Draw margin
     //   canvas.drawLine(margin,0, margin, getMeasuredHeight(), marginPaint);
       marginPaint.setTextSize(20);
        marginPaint.setTextAlign(Align.RIGHT);
        float w=0;
		int line=1;
		for(int i=0;i<str.length;i++)
		{
				try
				{
				//	System.out.println("Word!"+str[i]);
					if(str[i].trim().contains("2222222"))
					{
					//	System.out.println("Success!"+str[i]);
						w=0;
						line++;
						continue;
					}
					if(w+marginPaint.measureText(str[i]+" ")>this.getWidth())
					{
						w=0;
						line++;
						canvas.drawText(str[i], this.getWidth()-w, margin*line, marginPaint);
					//	canvas.drawText(str[i]+" ", this.getWidth()-w, margin, marginPaint);
						w+= marginPaint.measureText(str[i])+3;
					}
					else
					{
						canvas.drawText(str[i], this.getWidth()-w, margin*line, marginPaint);
						//canvas.drawText(str[i]+" ", this.getWidth()-w, 25*line, marginPaint);
						w+= marginPaint.measureText(str[i].trim())+3;
					}
				
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
			
		}
        


	    canvas.save();
        canvas.translate(margin, 0);
        
        // Use the TextView to render the text.
        super.onDraw(canvas);
        canvas.restore();
    }

	@Override
	public void setText(CharSequence text, BufferType type) {
		// TODO Auto-generated method stub
		  strText=text.toString();
	//	text=setNullText(text.length());
		  str=strText.split(" ");
		  
		super.setText(text, type);
	}
	private String setNullText(int length)
	{
		String str="";
		for(int i=0;i<length;i++)
		{
			str+=" ";
		}
		return str;
		
	}
}