import { TestApp } from '../TestApp'

let app: TestApp

beforeEach(() => {
	app = new TestApp()
})
afterEach(() => {
	app?.dispose()
})

describe('When editing text', () => {
	it('preserves the top center when center aligned', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				props: {
					text: 'Hello',
					align: 'middle',
					scale: 2,
				},
			},
		])
		const boundsA = app.getPageBoundsById(id)
		app.updateShapes([
			{
				id,
				type: 'text',
				props: {
					text: 'Hello\nworld!',
				},
			},
		])
		const boundsB = app.getPageBoundsById(id)
		expect(boundsA!.x).not.toEqual(boundsB!.x)
		expect(boundsA!.y).toEqual(boundsB!.y)
		expect(boundsA!.midX).toEqual(boundsB!.midX)
		expect(boundsA!.midY).not.toEqual(boundsB!.midY)
		expect(boundsA!.maxX).not.toEqual(boundsB!.maxX)
		expect(boundsA!.maxY).not.toEqual(boundsB!.maxY)
	})

	it('preserved the right center when center aligned and rotated 90deg', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				rotation: Math.PI / 2,
				props: {
					text: 'Hello',
					align: 'middle',
					scale: 2,
				},
			},
		])

		const boundsA = app.getPageBoundsById(id)!
		app.updateShapes([{ id, type: 'text', props: { text: 'Hello, world!' } }])
		const boundsB = app.getPageBoundsById(id)!
		expect(boundsA.x).toBeCloseTo(boundsB.x)
		expect(boundsA.y).not.toBeCloseTo(boundsB.y)
		expect(boundsA.midX).toBeCloseTo(boundsB.midX)
		expect(boundsA.midY).toBeCloseTo(boundsB.midY)
	})

	it('preserves the top left corner when start aligned', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				props: {
					text: 'Hello',
					align: 'start',
					scale: 2,
				},
			},
		])
		const boundsA = app.getPageBoundsById(id)
		app.updateShapes([
			{
				id,
				type: 'text',
				props: {
					text: 'Hello\nworld!',
				},
			},
		])
		const boundsB = app.getPageBoundsById(id)
		expect(boundsA!.x).toEqual(boundsB!.x)
		expect(boundsA!.y).toEqual(boundsB!.y)
		expect(boundsA!.midX).not.toEqual(boundsB!.midX)
		expect(boundsA!.midY).not.toEqual(boundsB!.midY)
		expect(boundsA!.maxX).not.toEqual(boundsB!.maxX)
		expect(boundsA!.maxY).not.toEqual(boundsB!.maxY)
	})

	it('preserves the top right edge when end aligned', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				props: {
					text: 'Hello',
					align: 'end',
					scale: 2,
				},
			},
		])
		const boundsA = app.getPageBoundsById(id)
		app.updateShapes([
			{
				id,
				type: 'text',
				props: {
					text: 'Hello\nworld!',
				},
			},
		])
		const boundsB = app.getPageBoundsById(id)
		expect(boundsA!.x).not.toEqual(boundsB!.x)
		expect(boundsA!.y).toEqual(boundsB!.y)
		expect(boundsA!.midX).not.toEqual(boundsB!.midX)
		expect(boundsA!.midY).not.toEqual(boundsB!.midY)
		expect(boundsA!.maxX).toEqual(boundsB!.maxX)
		expect(boundsA!.maxY).not.toEqual(boundsB!.maxY)
	})
})

describe('When changing text size', () => {
	it('preserves the center when center aligned', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				props: {
					text: 'Hello',
					size: 'm',
					align: 'middle',
					scale: 2,
				},
			},
		])
		const boundsA = app.getPageBoundsById(id)
		app.updateShapes([
			{
				id,
				type: 'text',
				props: {
					size: 'xl',
				},
			},
		])
		const boundsB = app.getPageBoundsById(id)
		expect(boundsA!.x).not.toEqual(boundsB!.x)
		expect(boundsA!.y).not.toEqual(boundsB!.y)
		expect(boundsA!.midX).toEqual(boundsB!.midX)
		expect(boundsA!.midY).toEqual(boundsB!.midY)
		expect(boundsA!.maxX).not.toEqual(boundsB!.maxX)
		expect(boundsA!.maxY).not.toEqual(boundsB!.maxY)
	})

	it('preserves the center left point when start aligned', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				props: {
					text: 'Hello',
					size: 'm',
					align: 'start',
					scale: 2,
				},
			},
		])
		const boundsA = app.getPageBoundsById(id)
		app.updateShapes([
			{
				id,
				type: 'text',
				props: {
					size: 'xl',
				},
			},
		])
		const boundsB = app.getPageBoundsById(id)
		expect(boundsA!.x).toEqual(boundsB!.x)
		expect(boundsA!.y).not.toEqual(boundsB!.y)
		expect(boundsA!.midX).not.toEqual(boundsB!.midX)
		expect(boundsA!.midY).toEqual(boundsB!.midY)
		expect(boundsA!.maxX).not.toEqual(boundsB!.maxX)
		expect(boundsA!.maxY).not.toEqual(boundsB!.maxY)
	})

	it('preserves the top right edge when end aligned', () => {
		const id = app.createShapeId()
		app.createShapes([
			{
				id,
				type: 'text',
				x: 0,
				y: 0,
				props: {
					text: 'Hello',
					size: 'm',
					align: 'end',
					scale: 2,
				},
			},
		])
		const boundsA = app.getPageBoundsById(id)
		app.updateShapes([
			{
				id,
				type: 'text',
				props: {
					size: 'xl',
				},
			},
		])
		const boundsB = app.getPageBoundsById(id)
		expect(boundsA!.x).not.toEqual(boundsB!.x)
		expect(boundsA!.y).not.toEqual(boundsB!.y)
		expect(boundsA!.midX).not.toEqual(boundsB!.midX)
		expect(boundsA!.midY).toEqual(boundsB!.midY)
		expect(boundsA!.maxX).toEqual(boundsB!.maxX)
		expect(boundsA!.maxY).not.toEqual(boundsB!.maxY)
	})
})

it('preserves the top left when the text has text', () => {
	const x = 0
	const y = 0
	const id = app.createShapeId()
	app.createShapes([
		{
			id,
			type: 'text',
			x: 0,
			y: 0,
			props: {
				text: 'Hello',
			},
		},
	])
	expect(app.getShapeById(id)).toMatchObject({
		x,
		y,
	})
})
