package net.yaht.m.dynview;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;

/**
 * Created by lisp on 5/9/16.
 */
public class PaintThread extends Thread {

    // our color palette;
    // Paint.ANTI_ALIAS_FLAG

    private SurfaceHolder _surf_h;
    private Rect _area;
    private Paint _paint;
    private Boolean _run_flag = false;

    public PaintThread(SurfaceHolder _surf_h, Rect _area, Paint _paint){

        this._surf_h = _surf_h;
        this._area = _area;
        this._paint = _paint;

    }

    public synchronized void _set_running(Boolean _run_flag) {

        this._run_flag = _run_flag;

    }

    @Override
    public void run() {

//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        int _start_x = 0;
        int _start_y = 0;
        int _stop_x = _area.right;
        int _stop_y = _area.bottom;

        int _y = _start_y;

        int col = Tools._random_color();

        while (true) {

            while (!_run_flag) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Canvas c = _surf_h.lockCanvas();

            for (int i = _start_y; i < _stop_y; i++) {

                if (i % 30 == 0) {

                    // = Tools._random_color();
                    do {
                        col = Tools._random_color();
                    } while (col == Color.BLACK);

                }

                _paint.setColor(col);
                c.drawLine(_start_x, i, _stop_x, i, _paint);
                // Tools.log2me("PaintThread/run(), i", Integer.toString(i));
            }

            _surf_h.unlockCanvasAndPost(c);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

    }
}
