package com.example.mountaintravel.view;


import com.smart.gtstrvl.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class LoadingView extends SurfaceView implements Callback, Runnable {

    private boolean m_bStart = false;
    private boolean m_bDraw = false;
    private int m_iSleepTime = 1000;
    private Thread m_thread = null;
    private SurfaceHolder m_surfaceholder;
    private Paint m_paint;
    private Paint m_paint1;
    private Canvas m_canvas;
    //private int SCREEN_WIDTH = 0;
    //private int SCREEN_HEIGHT = 0;
    //private float m_fScaleX = 0.0f;
    //private float m_fScaleY = 0.0f;
    //private Matrix m_matrix_bg;
    //private Matrix m_matrix_pos;
    private float m_fPosX = 0.0f;
    private float m_fPosY = 0.0f;
    private float m_width = 0.0f;
    public LoadingView(Context context)
    {
        super(context);
        init();
    }
    public LoadingView(Context context,float width)
    {
        super(context);
        init();
        m_width=width;
    }

    public LoadingView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }
    
    private void init()
    {
        m_surfaceholder = getHolder();
        m_surfaceholder.addCallback(this);
        m_paint = new Paint();
        m_paint.setAntiAlias(true);
        m_paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        m_paint.setDither(true);
        m_paint.setColor(Color.WHITE);
        m_paint.setStrokeWidth(30);
        m_paint1 = new Paint();
        m_paint1.setAntiAlias(true);
        m_paint1.setFlags(Paint.ANTI_ALIAS_FLAG);
        m_paint1.setDither(true);
        m_paint1.setColor(Color.GREEN);
        m_paint1.setStrokeWidth(30);
    }


    @Override
    public void run() {
        while (m_bStart)
        {
            if (m_bDraw) {
                draw();
                logc();
            }
            try
            {
                Thread.sleep(m_iSleepTime);
            }
            catch (InterruptedException e1)
            {
                e1.printStackTrace();
            }
        }
    }
    
    private void logc()
    {
        m_fPosX += 2.0f;
        //if (m_fPosX >= m_bmp_bk.getWidth() - 40) {
        if (m_fPosX >= m_width) {
            m_fPosX = 0.0f;
        }
    }

    private void draw()
    {
        m_canvas = m_surfaceholder.lockCanvas();
        try
        {
            if (m_canvas != null)
            {
                m_canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR); 
            	m_canvas.drawLine(0, 0, m_width, 0, m_paint);
                m_canvas.drawLine(0, 0, m_fPosX, 0, m_paint1);
            }

        } catch (Exception e)
        {

        } finally
        {
            if (m_canvas != null)
            {
                m_surfaceholder.unlockCanvasAndPost(m_canvas);
            }
        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //setZOrderMediaOverlay(true);
        //setZOrderOnTop(true);
        //getHolder().setFormat(PixelFormat.TRANSPARENT);
        
        //SCREEN_HEIGHT = getHeight();
        //SCREEN_WIDTH = getWidth();
        
   

        //m_fScaleX = (float) SCREEN_WIDTH / m_bmp_bk.getWidth();
        //m_fScaleY = (float) SCREEN_HEIGHT / m_bmp_bk.getHeight();

        //m_matrix_bg.setScale(m_fScaleX, m_fScaleY);
        m_bStart = true;
        if (m_thread!=null) {
			return;
		}
        if (m_thread == null) {
            m_thread = new Thread(this);
        }
        m_thread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        m_bStart = false;
    }
    
    public void start()
    {
        m_iSleepTime = 30;
        m_bDraw = true;
    }
    
    public void stop()
    {
        m_iSleepTime = 1000;
        m_fPosX = 0.0f;
        m_bDraw = false;
        m_bStart = false;
        try {
        	if (m_thread!=null) {
        		m_thread.join();
			}
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        m_thread = null;
    }
}
