local addonTable = select(2, ...)
---@type MKA
local MKA = addonTable.MKA
local core = addonTable

core.MeetingStoneFeedback = core.MeetingStoneFeedback or {}
local MeetingStoneFeedback = core.MeetingStoneFeedback
local MeetingStoneConstants = core.MeetingStoneConstants or {}
local MEETINGSTONE_ICON_EYE_SIZE = MeetingStoneConstants.MEETINGSTONE_ICON_EYE_SIZE
local MEETINGSTONE_ICON_EYE_HOST_SIZE = MeetingStoneConstants.MEETINGSTONE_ICON_EYE_HOST_SIZE
local MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE = MeetingStoneConstants.MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE
local MEETINGSTONE_ICON_EYE_BACKGROUND_MASK = MeetingStoneConstants.MEETINGSTONE_ICON_EYE_BACKGROUND_MASK
local MEETINGSTONE_EMPTY_EYE_FADE_DURATION = MeetingStoneConstants.MEETINGSTONE_EMPTY_EYE_FADE_DURATION
local MeetingStoneBridge = core.MeetingStoneBridge or {}

local TryLoadQueueStatusFrameUI = MeetingStoneBridge.TryLoadQueueStatusFrameUI

local function CreateMeetingStoneSuccessEye(parent)
    if not parent then
        return nil
    end
    if parent.mkaMeetingStoneSuccessEye then
        return parent.mkaMeetingStoneSuccessEye
    end

    local eye
    if TryLoadQueueStatusFrameUI() then
        local ok, created = pcall(CreateFrame, "Frame", nil, parent, "EyeTemplate")
        if ok and created and created.StartFoundAnimationInit then
            eye = created
            local eyeSize = parent and parent.mkaMeetingStoneEyeSize or MEETINGSTONE_ICON_EYE_SIZE
            eye:SetSize(eyeSize, eyeSize)
            eye:SetPoint("CENTER", parent, "CENTER", 0, 0)
            eye:SetFrameLevel(parent:GetFrameLevel() + 50)
            eye:Hide()
        end
    end

    parent.mkaMeetingStoneSuccessEye = eye
    return eye
end

local function StartMeetingStoneEmptyApplicantEyeLoop(eye)
    if not eye then
        return false
    end

    local nativeEye = eye.Eye or eye
    if nativeEye.StartSearchingAnimation then
        eye.mkaMeetingStoneEyeLooping = true
        pcall(nativeEye.StartSearchingAnimation, nativeEye)
        return true
    end
    if nativeEye.StartFoundAnimationLoop then
        eye.mkaMeetingStoneEyeLooping = true
        pcall(nativeEye.StartFoundAnimationLoop, nativeEye)
        return true
    end
    if eye.StartSearchingAnimation then
        eye.mkaMeetingStoneEyeLooping = true
        pcall(eye.StartSearchingAnimation, eye)
        return true
    end
    if eye.StartFoundAnimationLoop then
        eye.mkaMeetingStoneEyeLooping = true
        pcall(eye.StartFoundAnimationLoop, eye)
        return true
    end
    return false
end

function MKA:GetMeetingStoneEyeParent()
    local frame = self.mainWindow
    local portraitContainer = frame and frame.PortraitContainer
    local parent = portraitContainer or frame
    local anchor = portraitContainer and portraitContainer.portrait or parent
    if not parent then
        local widgets = self.widgets
        return widgets and (widgets.root or widgets.MSRightPanel)
    end

    if not parent.mkaMeetingStoneEyeHost then
        local host = CreateFrame("Frame", nil, parent)
        host:SetFrameLevel((parent.GetFrameLevel and parent:GetFrameLevel() or 0) + 30)
        host.mkaMeetingStoneEyeSize = MEETINGSTONE_ICON_EYE_SIZE

        local background = host:CreateTexture(nil, "BACKGROUND")
        background:SetPoint("CENTER")
        background:SetTexture("Interface\\Buttons\\WHITE8X8")
        background:SetVertexColor(0, 0, 0, 1)
        if host.CreateMaskTexture and background.AddMaskTexture then
            local mask = host:CreateMaskTexture()
            mask:SetTexture(MEETINGSTONE_ICON_EYE_BACKGROUND_MASK, "CLAMPTOBLACKADDITIVE", "CLAMPTOBLACKADDITIVE")
            mask:SetPoint("CENTER", background, "CENTER", 0, 0)
            mask:SetSize(MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE, MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE)
            pcall(background.AddMaskTexture, background, mask)
            host.BlackFillMask = mask
        elseif portraitContainer and portraitContainer.CircleMask and background.AddMaskTexture then
            pcall(background.AddMaskTexture, background, portraitContainer.CircleMask)
        end
        background:Hide()
        host.BlackFill = background

        parent.mkaMeetingStoneEyeHost = host
    end

    parent.mkaMeetingStoneEyeHost:ClearAllPoints()
    parent.mkaMeetingStoneEyeHost:SetSize(MEETINGSTONE_ICON_EYE_HOST_SIZE, MEETINGSTONE_ICON_EYE_HOST_SIZE)
    parent.mkaMeetingStoneEyeHost:SetPoint("CENTER", anchor, "CENTER", 0, 0)
    if parent.mkaMeetingStoneEyeHost.BlackFill then
        parent.mkaMeetingStoneEyeHost.BlackFill:SetSize(MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE, MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE)
    end
    if parent.mkaMeetingStoneEyeHost.BlackFillMask then
        parent.mkaMeetingStoneEyeHost.BlackFillMask:SetSize(MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE, MEETINGSTONE_ICON_EYE_BACKGROUND_SIZE)
    end

    return parent.mkaMeetingStoneEyeHost
end

function MKA:ShowMeetingStoneEmptyApplicantEye(playSound, playInitial)
    local parent = self:GetMeetingStoneEyeParent()
    local eye = CreateMeetingStoneSuccessEye(parent)
    if not eye then
        return
    end

    eye:ClearAllPoints()
    eye:SetPoint("CENTER", parent, "CENTER", 0, 0)
    eye:SetSize(parent.mkaMeetingStoneEyeSize or MEETINGSTONE_ICON_EYE_SIZE, parent.mkaMeetingStoneEyeSize or MEETINGSTONE_ICON_EYE_SIZE)
    eye:SetAlpha(1)
    eye:SetScale(1)
    if parent.BlackFill then
        parent.BlackFill:Show()
    end
    parent:Show()
    eye:Show()

    if eye.mkaFadeAnimation then
        eye.mkaFadeAnimation:Stop()
    end
    if eye.mkaMeetingStoneEyeLooping then
        return
    end

    if eye.StopAnimating then
        pcall(eye.StopAnimating, eye)
    end

    StartMeetingStoneEmptyApplicantEyeLoop(eye)
end

function MKA:SetMeetingStoneQueueStatusGlow(enabled)
    local parent = self:GetMeetingStoneEyeParent()
    local eye = parent and parent.mkaMeetingStoneSuccessEye
    if not eye then
        return
    end

    if eye.SetGlowLock then
        pcall(eye.SetGlowLock, eye, "mka-meetingstone-applicant", enabled == true, 3)
        return
    end

    if _G.QueueStatusButton and _G.QueueStatusButton.SetGlowLock then
        pcall(_G.QueueStatusButton.SetGlowLock, _G.QueueStatusButton, "mka-meetingstone-applicant", enabled == true, 3)
        return
    end

    local highlight = eye.EyeHighlightAnim
    if not highlight then
        return
    end

    if enabled then
        if not highlight:IsPlaying() then
            highlight:Play()
        end
    elseif highlight:IsPlaying() then
        highlight:Stop()
    end
end

function MKA:FadeOutMeetingStoneEmptyApplicantEye()
    local parent = self:GetMeetingStoneEyeParent()
    local eye = parent and parent.mkaMeetingStoneSuccessEye
    if not eye or not eye:IsShown() then
        return
    end

    self:SetMeetingStoneQueueStatusGlow(false)
    eye.mkaMeetingStoneEyeLooping = nil
    if not eye.mkaFadeAnimation then
        local fade = eye:CreateAnimationGroup()
        local alpha = fade:CreateAnimation("Alpha")
        alpha:SetOrder(1)
        alpha:SetDuration(MEETINGSTONE_EMPTY_EYE_FADE_DURATION)
        alpha:SetFromAlpha(1)
        alpha:SetToAlpha(0)
        alpha:SetSmoothing("IN")
        fade:SetScript("OnFinished", function()
            local nativeEye = eye.Eye or eye
            if nativeEye.StopAnimating then
                pcall(nativeEye.StopAnimating, nativeEye)
            elseif eye.StopAnimating then
                pcall(eye.StopAnimating, eye)
            end
            eye:SetAlpha(1)
            eye:SetScale(1)
            eye:Hide()
            if parent.BlackFill then
                parent.BlackFill:Hide()
            end
        end)
        eye.mkaFadeAnimation = fade
    end

    eye.mkaFadeAnimation:Stop()
    eye.mkaFadeAnimation:Play()
end

function MKA:StopMeetingStoneCreateSuccessFeedback()
    local parent = self:GetMeetingStoneEyeParent()
    local eye = parent and parent.mkaMeetingStoneSuccessEye
    if not eye then
        return
    end

    self:SetMeetingStoneQueueStatusGlow(false)
    eye.mkaMeetingStoneEyeLooping = nil
    if eye.mkaFadeAnimation then
        eye.mkaFadeAnimation:Stop()
    end
    local nativeEye = eye.Eye or eye
    if nativeEye.StopAnimating then
        pcall(nativeEye.StopAnimating, nativeEye)
    elseif eye.StopAnimating then
        pcall(eye.StopAnimating, eye)
    end
    eye:SetAlpha(1)
    eye:SetScale(1)
    eye:Hide()
    if parent.BlackFill then
        parent.BlackFill:Hide()
    end
end

function MKA:UpdateMeetingStoneEmptyApplicantEye(applicantRowsCount, rawApplicantCount)
    local hasActiveEntry = false
    if C_LFGList and C_LFGList.HasActiveEntryInfo then
        local ok, activeEntry = pcall(C_LFGList.HasActiveEntryInfo)
        hasActiveEntry = ok and activeEntry == true
    end
    if not hasActiveEntry then
        self:FadeOutMeetingStoneEmptyApplicantEye()
        return
    end

    if rawApplicantCount == nil and C_LFGList and C_LFGList.GetApplicants then
        local ok, applicants = pcall(C_LFGList.GetApplicants)
        rawApplicantCount = ok and type(applicants) == "table" and #applicants or 0
    end

    self:ShowMeetingStoneEmptyApplicantEye(false, false)
    self:SetMeetingStoneQueueStatusGlow((rawApplicantCount or 0) > 0)
end

function MKA:PlayMeetingStoneCreateSuccessFeedback()
    self:ShowMeetingStoneEmptyApplicantEye(true, true)
end


MeetingStoneFeedback.CreateSuccessEye = CreateMeetingStoneSuccessEye
MeetingStoneFeedback.StartEmptyApplicantEyeLoop = StartMeetingStoneEmptyApplicantEyeLoop
