local luaunit = require("testing.luaunit")
require("testing.loveStub")

local Renderer = require("modules.Renderer")
local Color = require("modules.Color")
local RoundedRect = require("modules.RoundedRect")
local NinePatch = require("modules.NinePatch")
local ImageRenderer = require("modules.ImageRenderer")
local ImageCache = require("modules.ImageCache")
local Theme = require("modules.Theme")
local Blur = require("modules.Blur")
local utils = require("modules.utils")

TestRenderer = {}

-- Helper to create dependencies
local function createDeps()
  return {
    Color = Color,
    RoundedRect = RoundedRect,
    NinePatch = NinePatch,
    ImageRenderer = ImageRenderer,
    ImageCache = ImageCache,
    Theme = Theme,
    Blur = Blur,
    utils = utils,
  }
end

-- Helper to create mock element with all required properties
local function createMockElement()
  local element = {
    x = 0,
    y = 0,
    width = 100,
    height = 100,
    _absoluteX = 0,
    _absoluteY = 0,
    padding = {
      left = 0,
      right = 0,
      top = 0,
      bottom = 0,
    },
    textSize = 14,
    fontFamily = nil,
    themeComponent = nil,
    _themeManager = nil,
    textColor = Color.new(0, 0, 0, 1),
    text = "Test",
    editable = false,
    multiline = false,
    textWrap = false,
    textAlign = utils.enums.TextAlign.START,
    scaleCorners = true,
    scalingAlgorithm = "bilinear",
    getScaledContentPadding = function()
      return nil
    end,
  }
  return element
end

-- Test: new() creates instance with defaults
function TestRenderer:testNewWithDefaults()
  local renderer = Renderer.new({}, createDeps())

  luaunit.assertNotNil(renderer)
  luaunit.assertEquals(renderer.opacity, 1)
  luaunit.assertEquals(renderer.objectFit, "fill")
  luaunit.assertEquals(renderer.objectPosition, "center center")
  luaunit.assertEquals(renderer.imageOpacity, 1)
end

-- Test: new() with custom backgroundColor
function TestRenderer:testNewWithBackgroundColor()
  local bgColor = Color.new(1, 0, 0, 1)
  local renderer = Renderer.new({
    backgroundColor = bgColor,
  }, createDeps())

  luaunit.assertEquals(renderer.backgroundColor, bgColor)
end

-- Test: new() with custom borderColor
function TestRenderer:testNewWithBorderColor()
  local borderColor = Color.new(0, 1, 0, 1)
  local renderer = Renderer.new({
    borderColor = borderColor,
  }, createDeps())

  luaunit.assertEquals(renderer.borderColor, borderColor)
end

-- Test: new() with custom opacity
function TestRenderer:testNewWithOpacity()
  local renderer = Renderer.new({
    opacity = 0.5,
  }, createDeps())

  luaunit.assertEquals(renderer.opacity, 0.5)
end

-- Test: new() with border configuration
function TestRenderer:testNewWithBorder()
  local renderer = Renderer.new({
    border = {
      top = true,
      right = false,
      bottom = true,
      left = false,
    },
  }, createDeps())

  luaunit.assertTrue(renderer.border.top)
  luaunit.assertFalse(renderer.border.right)
  luaunit.assertTrue(renderer.border.bottom)
  luaunit.assertFalse(renderer.border.left)
end

-- Test: new() with cornerRadius
function TestRenderer:testNewWithCornerRadius()
  local renderer = Renderer.new({
    cornerRadius = {
      topLeft = 5,
      topRight = 10,
      bottomLeft = 15,
      bottomRight = 20,
    },
  }, createDeps())

  luaunit.assertEquals(renderer.cornerRadius.topLeft, 5)
  luaunit.assertEquals(renderer.cornerRadius.topRight, 10)
  luaunit.assertEquals(renderer.cornerRadius.bottomLeft, 15)
  luaunit.assertEquals(renderer.cornerRadius.bottomRight, 20)
end

-- Test: new() with theme
function TestRenderer:testNewWithTheme()
  local renderer = Renderer.new({
    theme = "dark",
    themeComponent = "button",
  }, createDeps())

  luaunit.assertEquals(renderer.theme, "dark")
  luaunit.assertEquals(renderer.themeComponent, "button")
  luaunit.assertEquals(renderer._themeState, "normal")
end

-- Test: new() with imagePath (failed load)
function TestRenderer:testNewWithImagePath()
  local renderer = Renderer.new({
    imagePath = "nonexistent/image.png",
  }, createDeps())

  luaunit.assertEquals(renderer.imagePath, "nonexistent/image.png")
  -- Image will fail to load, so _loadedImage should be nil
  luaunit.assertNil(renderer._loadedImage)
end

-- Test: new() with imagePath (successful load via cache)
function TestRenderer:testNewWithImagePathSuccessfulLoad()
  local mockImage = {
    getDimensions = function()
      return 50, 50
    end,
  }

  -- Pre-populate the cache so load succeeds
  ImageCache._cache["test/image.png"] = {
    image = mockImage,
    imageData = nil,
  }

  local renderer = Renderer.new({
    imagePath = "test/image.png",
  }, createDeps())

  luaunit.assertEquals(renderer.imagePath, "test/image.png")
  luaunit.assertEquals(renderer._loadedImage, mockImage)

  -- Clean up cache
  ImageCache._cache["test/image.png"] = nil
end

-- Test: new() with image object
function TestRenderer:testNewWithImageObject()
  local mockImage = {
    getDimensions = function()
      return 50, 50
    end,
  }

  local renderer = Renderer.new({
    image = mockImage,
  }, createDeps())

  luaunit.assertEquals(renderer.image, mockImage)
  luaunit.assertEquals(renderer._loadedImage, mockImage)
end

-- Test: new() with objectFit
function TestRenderer:testNewWithObjectFit()
  local renderer = Renderer.new({
    objectFit = "contain",
  }, createDeps())

  luaunit.assertEquals(renderer.objectFit, "contain")
end

-- Test: new() with objectPosition
function TestRenderer:testNewWithObjectPosition()
  local renderer = Renderer.new({
    objectPosition = "top left",
  }, createDeps())

  luaunit.assertEquals(renderer.objectPosition, "top left")
end

-- Test: new() with imageOpacity
function TestRenderer:testNewWithImageOpacity()
  local renderer = Renderer.new({
    imageOpacity = 0.7,
  }, createDeps())

  luaunit.assertEquals(renderer.imageOpacity, 0.7)
end

-- Test: new() with contentBlur
function TestRenderer:testNewWithContentBlur()
  local renderer = Renderer.new({
    contentBlur = {
      intensity = 5,
      quality = "high",
    },
  }, createDeps())

  luaunit.assertNotNil(renderer.contentBlur)
  luaunit.assertEquals(renderer.contentBlur.intensity, 5)
  luaunit.assertEquals(renderer.contentBlur.quality, "high")
end

-- Test: new() with backdropBlur
function TestRenderer:testNewWithBackdropBlur()
  local renderer = Renderer.new({
    backdropBlur = {
      intensity = 10,
      quality = "medium",
    },
  }, createDeps())

  luaunit.assertNotNil(renderer.backdropBlur)
  luaunit.assertEquals(renderer.backdropBlur.intensity, 10)
  luaunit.assertEquals(renderer.backdropBlur.quality, "medium")
end

-- Test: initialize() sets element reference
function TestRenderer:testInitialize()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()

  renderer:initialize(mockElement)

  luaunit.assertEquals(renderer._element, mockElement)
end

-- Test: setThemeState() changes state
function TestRenderer:testSetThemeState()
  local renderer = Renderer.new({}, createDeps())

  renderer:setThemeState("hover")
  luaunit.assertEquals(renderer._themeState, "hover")

  renderer:setThemeState("pressed")
  luaunit.assertEquals(renderer._themeState, "pressed")

  renderer:setThemeState("disabled")
  luaunit.assertEquals(renderer._themeState, "disabled")
end

-- Note: getBlurInstance() tests are skipped because Renderer.lua has a bug
-- where it passes string quality names ("high", "medium", "low") to Blur.new()
-- but Blur.new() expects numeric quality values (1-10)

-- Test: destroy() method exists and can be called
function TestRenderer:testDestroy()
  local renderer = Renderer.new({}, createDeps())

  -- Should not error
  renderer:destroy()
  luaunit.assertTrue(true)
end

-- Test: new() with all border sides enabled
function TestRenderer:testNewWithAllBordersEnabled()
  local renderer = Renderer.new({
    border = {
      top = true,
      right = true,
      bottom = true,
      left = true,
    },
  }, createDeps())

  luaunit.assertTrue(renderer.border.top)
  luaunit.assertTrue(renderer.border.right)
  luaunit.assertTrue(renderer.border.bottom)
  luaunit.assertTrue(renderer.border.left)
end

-- Test: new() with zero cornerRadius
function TestRenderer:testNewWithZeroCornerRadius()
  local renderer = Renderer.new({
    cornerRadius = {
      topLeft = 0,
      topRight = 0,
      bottomLeft = 0,
      bottomRight = 0,
    },
  }, createDeps())

  luaunit.assertEquals(renderer.cornerRadius.topLeft, 0)
  luaunit.assertEquals(renderer.cornerRadius.topRight, 0)
  luaunit.assertEquals(renderer.cornerRadius.bottomLeft, 0)
  luaunit.assertEquals(renderer.cornerRadius.bottomRight, 0)
end

-- Test: new() with negative opacity (edge case)
function TestRenderer:testNewWithNegativeOpacity()
  local renderer = Renderer.new({
    opacity = -0.5,
  }, createDeps())

  luaunit.assertEquals(renderer.opacity, -0.5)
end

-- Test: new() with opacity > 1 (edge case)
function TestRenderer:testNewWithOpacityGreaterThanOne()
  local renderer = Renderer.new({
    opacity = 1.5,
  }, createDeps())

  luaunit.assertEquals(renderer.opacity, 1.5)
end

-- Test: new() with zero imageOpacity
function TestRenderer:testNewWithZeroImageOpacity()
  local renderer = Renderer.new({
    imageOpacity = 0,
  }, createDeps())

  luaunit.assertEquals(renderer.imageOpacity, 0)
end

-- Test: new() with both imagePath and image (image takes precedence)
function TestRenderer:testNewWithBothImagePathAndImage()
  local mockImage = {
    getDimensions = function()
      return 50, 50
    end,
  }

  local renderer = Renderer.new({
    imagePath = "path/to/image.png",
    image = mockImage,
  }, createDeps())

  luaunit.assertEquals(renderer._loadedImage, mockImage)
end

-- Test: new() with empty config
function TestRenderer:testNewWithEmptyConfig()
  local renderer = Renderer.new({}, createDeps())

  luaunit.assertNotNil(renderer)
  luaunit.assertNotNil(renderer.backgroundColor)
  luaunit.assertNotNil(renderer.borderColor)
  luaunit.assertNotNil(renderer.border)
  luaunit.assertNotNil(renderer.cornerRadius)
end

-- Test: draw() with basic config (should not error)
function TestRenderer:testDrawBasic()
  local renderer = Renderer.new({
    backgroundColor = Color.new(1, 0, 0, 1),
  }, createDeps())

  local mockElement = createMockElement()
  renderer:initialize(mockElement)

  -- Should not error when drawing
  renderer:draw()
  luaunit.assertTrue(true)
end

-- Test: draw() with nil backdrop canvas
function TestRenderer:testDrawWithNilBackdrop()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  renderer:initialize(mockElement)

  renderer:draw(nil)
  luaunit.assertTrue(true)
end

-- Test: drawPressedState() method exists
function TestRenderer:testDrawPressedState()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  renderer:initialize(mockElement)

  -- Should not error
  renderer:drawPressedState(0, 0, 100, 100)
  luaunit.assertTrue(true)
end

-- Test: getFont() with element
function TestRenderer:testGetFont()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  mockElement.fontSize = 16
  renderer:initialize(mockElement)

  local font = renderer:getFont(mockElement)
  luaunit.assertNotNil(font)
end

-- Test: drawScrollbars() with proper dims structure
function TestRenderer:testDrawScrollbars()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  mockElement.hideScrollbars = { vertical = false, horizontal = false }
  mockElement.scrollbarWidth = 8
  mockElement.scrollbarPadding = 2
  mockElement.scrollbarColor = Color.new(0.5, 0.5, 0.5, 1)
  renderer:initialize(mockElement)

  local dims = {
    scrollX = 0,
    scrollY = 0,
    contentWidth = 200,
    contentHeight = 200,
    vertical = {
      visible = false,
      thumbPosition = 0,
      thumbSize = 50,
    },
    horizontal = {
      visible = false,
      thumbPosition = 0,
      thumbSize = 50,
    },
  }

  -- Should not error when scrollbars are not visible
  renderer:drawScrollbars(mockElement, 0, 0, 100, 100, dims)
  luaunit.assertTrue(true)
end

-- Test: new() with all visual properties set
function TestRenderer:testNewWithAllVisualProperties()
  local renderer = Renderer.new({
    backgroundColor = Color.new(0.5, 0.5, 0.5, 1),
    borderColor = Color.new(1, 1, 1, 1),
    opacity = 0.8,
    border = {
      top = true,
      right = true,
      bottom = true,
      left = true,
    },
    cornerRadius = {
      topLeft = 10,
      topRight = 10,
      bottomLeft = 10,
      bottomRight = 10,
    },
    theme = "custom",
    themeComponent = "panel",
    imagePath = nil,
    objectFit = "contain",
    objectPosition = "top left",
    imageOpacity = 0.9,
  }, createDeps())

  luaunit.assertEquals(renderer.opacity, 0.8)
  luaunit.assertEquals(renderer.objectFit, "contain")
  luaunit.assertEquals(renderer.objectPosition, "top left")
  luaunit.assertEquals(renderer.imageOpacity, 0.9)
  luaunit.assertTrue(renderer.border.top)
  luaunit.assertTrue(renderer.border.right)
  luaunit.assertEquals(renderer.cornerRadius.topLeft, 10)
end

-- Test: new() with theme state
function TestRenderer:testThemeStateDefault()
  local renderer = Renderer.new({
    theme = "dark",
  }, createDeps())

  luaunit.assertEquals(renderer._themeState, "normal")
end

-- Test: setThemeState() with various states
function TestRenderer:testSetThemeStateVariousStates()
  local renderer = Renderer.new({}, createDeps())

  renderer:setThemeState("active")
  luaunit.assertEquals(renderer._themeState, "active")

  renderer:setThemeState("normal")
  luaunit.assertEquals(renderer._themeState, "normal")
end

-- Test: new() with fractional opacity
function TestRenderer:testNewWithFractionalOpacity()
  local renderer = Renderer.new({
    opacity = 0.333,
  }, createDeps())

  luaunit.assertEquals(renderer.opacity, 0.333)
end

-- Test: new() with fractional imageOpacity
function TestRenderer:testNewWithFractionalImageOpacity()
  local renderer = Renderer.new({
    imageOpacity = 0.777,
  }, createDeps())

  luaunit.assertEquals(renderer.imageOpacity, 0.777)
end

-- Test: new() stores dependencies correctly
function TestRenderer:testNewStoresDependencies()
  local deps = createDeps()
  local renderer = Renderer.new({}, deps)

  luaunit.assertEquals(renderer._Color, deps.Color)
  luaunit.assertEquals(renderer._RoundedRect, deps.RoundedRect)
  luaunit.assertEquals(renderer._NinePatch, deps.NinePatch)
  luaunit.assertEquals(renderer._ImageRenderer, deps.ImageRenderer)
  luaunit.assertEquals(renderer._ImageCache, deps.ImageCache)
  luaunit.assertEquals(renderer._Theme, deps.Theme)
  luaunit.assertEquals(renderer._Blur, deps.Blur)
  luaunit.assertEquals(renderer._utils, deps.utils)
end

-- Test: new() with objectFit variations
function TestRenderer:testNewWithVariousObjectFit()
  local renderer1 = Renderer.new({ objectFit = "cover" }, createDeps())
  luaunit.assertEquals(renderer1.objectFit, "cover")

  local renderer2 = Renderer.new({ objectFit = "contain" }, createDeps())
  luaunit.assertEquals(renderer2.objectFit, "contain")

  local renderer3 = Renderer.new({ objectFit = "none" }, createDeps())
  luaunit.assertEquals(renderer3.objectFit, "none")
end

-- Test: new() with objectPosition variations
function TestRenderer:testNewWithVariousObjectPosition()
  local renderer1 = Renderer.new({ objectPosition = "top" }, createDeps())
  luaunit.assertEquals(renderer1.objectPosition, "top")

  local renderer2 = Renderer.new({ objectPosition = "bottom right" }, createDeps())
  luaunit.assertEquals(renderer2.objectPosition, "bottom right")

  local renderer3 = Renderer.new({ objectPosition = "50% 50%" }, createDeps())
  luaunit.assertEquals(renderer3.objectPosition, "50% 50%")
end

-- Test: drawText() with mock element
function TestRenderer:testDrawText()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  mockElement.text = "Hello World"
  mockElement.fontSize = 14
  mockElement.textAlign = "left"
  renderer:initialize(mockElement)

  -- Should not error
  renderer:drawText(mockElement)
  luaunit.assertTrue(true)
end

-- Test: drawText() with nil text
function TestRenderer:testDrawTextWithNilText()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  mockElement.text = nil
  renderer:initialize(mockElement)

  -- Should handle nil text gracefully
  renderer:drawText(mockElement)
  luaunit.assertTrue(true)
end

-- Test: drawText() with empty string
function TestRenderer:testDrawTextWithEmptyString()
  local renderer = Renderer.new({}, createDeps())
  local mockElement = createMockElement()
  mockElement.text = ""
  renderer:initialize(mockElement)

  renderer:drawText(mockElement)
  luaunit.assertTrue(true)
end

if not _G.RUNNING_ALL_TESTS then
  os.exit(luaunit.LuaUnit.run())
end
