using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DrawPaintbrush : DrawShape
{
    private RawImage drawImage_;
    private RectTransform rectTransform_;

    Vector2 prevPos_;

    private void Start()
    {
        drawImage_ = GetComponent<RawImage>();
        rectTransform_ = GetComponent<RectTransform>();
        PrepareDrawTexture();
    }

    public override void StartDraw(Vector2 position)
    {
        prevPos_ = rectTransform_.InverseTransformPoint(position);
    }
    public override void StopDraw(Vector2 position)
    {
    }
    public override void UpdateShape(Vector2 newPosition)
    {
        Vector2 position = rectTransform_.InverseTransformPoint(newPosition);
        Texture2D tex = (Texture2D)drawImage_.texture;
        DrawLine(tex, (int)position.x, (int)position.y, (int)prevPos_.x, (int)prevPos_.y, Color.black);

        prevPos_ = position;
    }
    public override void Clear()
    {
        ClearDrawTexture(drawImage_.texture as Texture2D);
    }

    private void PrepareDrawTexture()
    {
        int WIDTH = (int)rectTransform_.rect.width;
        int HEIGHT = (int)rectTransform_.rect.height;

        Texture2D tex = new Texture2D(WIDTH, HEIGHT, TextureFormat.ARGB32, false);

        ClearDrawTexture(tex);

        drawImage_.texture = tex;
    }

    private void ClearDrawTexture(Texture2D tex)
    {
        Color fillColor = Color.clear;
        Color[] fillPixels = new Color[tex.width * tex.height];

        for (int i = 0; i < fillPixels.Length; i++)
        {
            fillPixels[i] = fillColor;
        }
        tex.SetPixels(fillPixels);
        tex.Apply();
    }

    private void DrawLine(Texture2D tex, int x0, int y0, int x1, int y1, Color col)
    {
        int dy = (int)(y1 - y0);
        int dx = (int)(x1 - x0);
        int stepx, stepy;

        if (dy < 0) { dy = -dy; stepy = -1; }
        else { stepy = 1; }
        if (dx < 0) { dx = -dx; stepx = -1; }
        else { stepx = 1; }
        dy <<= 1;
        dx <<= 1;

        float fraction = 0;

        tex.SetPixel(x0, y0, col);
        if (dx > dy)
        {
            fraction = dy - (dx >> 1);
            while (Mathf.Abs(x0 - x1) > 1)
            {
                if (fraction >= 0)
                {
                    y0 += stepy;
                    fraction -= dx;
                }
                x0 += stepx;
                fraction += dy;
                tex.SetPixel(x0, y0, col);
                tex.SetPixel(x0 - 1, y0, col);
                tex.SetPixel(x0 + 1, y0, col);
                tex.SetPixel(x0, y0 - 1, col);
                tex.SetPixel(x0, y0 + 1, col);
            }
        }
        else
        {
            fraction = dx - (dy >> 1);
            while (Mathf.Abs(y0 - y1) > 1)
            {
                if (fraction >= 0)
                {
                    x0 += stepx;
                    fraction -= dy;
                }
                y0 += stepy;
                fraction += dx;
                tex.SetPixel(x0, y0, col);
                tex.SetPixel(x0 - 1, y0, col);
                tex.SetPixel(x0 + 1, y0, col);
                tex.SetPixel(x0, y0 - 1, col);
                tex.SetPixel(x0, y0 + 1, col);
            }
        }
        tex.Apply();
    }
}