package bitmap

import (
	"errors"
	"fmt"
	"math"
	"math/bits"
)

const intSize = 32

type Bitmap struct {
	width     uint32
	height    uint32
	bitVector []uint32
}

func NewBitmap(width uint32, height uint32) Bitmap {
	return Bitmap{width: width, height: height, bitVector: make([]uint32, int(math.Ceil(float64(width*height)/intSize)))}
}

func (p Bitmap) Set(x, y int) {
	boundX := (p.width + uint32(x)) % p.width
	boundY := (p.height + uint32(y)) % p.height
	p.set(boundX + boundY*p.width)
}

func (p Bitmap) set(number uint32) {
	p.bitVector[number/intSize] |= (0b1 << (number % intSize))
}

func (p Bitmap) IsSet(x, y int) bool {
	return p.isSet(uint32(x) + uint32(y)*p.width)
}

func (p Bitmap) isSet(number uint32) bool {
	return p.bitVector[number/intSize]&(0b1<<(number%intSize)) > 0
}

func (p Bitmap) FreeCount() (result int) {
	result = int(p.width) * int(p.height)
	for i := range p.bitVector {
		result -= bits.OnesCount32(p.bitVector[i])
	}
	return
}

func (p Bitmap) GetFreeN(number int) (int, int, error) {
	if p.FreeCount() == 0 {
		return 0, 0, errors.New("no free pixels left")
	}
	fc := 0
	n := 0
	for ; n < int(p.width)*int(p.height); n++ {
		if p.isSet(uint32(n)) {
			continue
		}
		if fc == number {
			y := n / int(p.height)
			x := n - int(p.height)*y
			return x, y, nil
		}
		fc++
	}
	return 0, 0, fmt.Errorf("get free pixel: no such number=%d, max=%d", number, n)
}

func (p Bitmap) SetFreeN(number int) error {
	if p.FreeCount() == 0 {
		return errors.New("no free pixels left")
	}
	fc := 0
	n := 0
	for ; n < int(p.width)*int(p.height); n++ {
		if p.isSet(uint32(n)) {
			continue
		}
		if fc == number {
			p.set(uint32(n))
			return nil
		}
		fc++
	}
	return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n)
}

func (p Bitmap) Circle(x, y int, r float64, fill bool) {
	plotX := 0
	plotY := int(math.Ceil(r))
	delta := 3 - 2*plotY
	lastY := plotY
	for plotX <= plotY {
		p.octant(x, y, plotX, plotY)
		if fill && plotY < lastY {
			for lineX := 0; lineX < plotX; lineX++ {
				p.octant(x, y, lineX, plotY)
			}
			lastY = plotY
		}
		if delta < 0 {
			delta += 4*plotX + 6
		} else {
			delta += 4*(plotX-plotY) + 10
			plotY -= 1
		}
		plotX += 1
	}
	if !fill {
		return
	}
	for fillX := 0; fillX < plotX; fillX++ {
		for fillY := 0; fillY <= fillX; fillY++ {
			p.octant(x, y, fillX, fillY)
		}
	}
}

func (p Bitmap) circleAdjacent(x, y int, r float64) {
	plotX := 0
	plotY := int(math.Ceil(r))
	delta := 1 - 2*plotY
	err := 0
	for plotX <= plotY {
		p.octant(x, y, plotX, plotY)
		err = 2*(delta+plotY) - 1
		if delta < 0 && err <= 0 {
			plotX += 1
			delta += 2*plotX + 1
			continue
		}
		if delta > 0 && err > 0 {
			plotY -= 1
			delta -= 2*plotY + 1
			continue
		}
		plotX += 1
		plotY -= 1
		delta += 2 * (plotX - plotY)
	}
}

func (p Bitmap) octant(x, y int, plotX, plotY int) {
	p.Set(x+plotX, y+plotY)
	p.Set(x+plotX, y-plotY)
	p.Set(x-plotX, y+plotY)
	p.Set(x-plotX, y-plotY)
	p.Set(x+plotY, y+plotX)
	p.Set(x+plotY, y-plotX)
	p.Set(x-plotY, y+plotX)
	p.Set(x-plotY, y-plotX)
}

func (p Bitmap) Clear() {
	for i := range p.bitVector {
		p.bitVector[i] &= 0
	}
}

func (p Bitmap) String() string {
	px := map[bool]string{true: "██", false: "░░"}
	var result string
	cnt := 0
	for i := 0; i < len(p.bitVector); i++ {
		for bit := 0; bit < intSize && cnt < int(p.width*p.height); bit++ {
			result += px[p.bitVector[i]&(0b1<<bit) > 0]
			cnt++
			if cnt%int(p.width) == 0 {
				result += "\n"
			}
		}
	}
	return result
}
