package commands

import (
	"fmt"
	"strings"

	"c65gm/internal/compiler"
	"c65gm/internal/preproc"
	"c65gm/internal/utils"
)

// EndSwitchCommand handles ENDSWITCH statements
// Syntax: ENDSWITCH
type EndSwitchCommand struct {
}

func (c *EndSwitchCommand) WillHandle(line preproc.Line) bool {
	params, err := utils.ParseParams(line.Text)
	if err != nil || len(params) == 0 {
		return false
	}
	return strings.ToUpper(params[0]) == "ENDSWITCH"
}

func (c *EndSwitchCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext) error {
	params, err := utils.ParseParams(line.Text)
	if err != nil {
		return err
	}

	if len(params) != 1 {
		return fmt.Errorf("ENDSWITCH: expected 1 parameter (ENDSWITCH), got %d", len(params))
	}

	// Check if we're inside a SWITCH
	if ctx.SwitchStack.IsEmpty() {
		return fmt.Errorf("ENDSWITCH: not inside a SWITCH statement")
	}

	return nil
}

func (c *EndSwitchCommand) Generate(ctx *compiler.CompilerContext) ([]string, error) {
	switchInfo, err := ctx.SwitchStack.Pop()
	if err != nil {
		return nil, fmt.Errorf("ENDSWITCH: %w", err)
	}

	var asm []string

	// If there's pending code (last CASE without DEFAULT), emit skip label
	if switchInfo.NeedsPendingCode {
		asm = append(asm, switchInfo.PendingSkipLabel)
	}

	// Emit the ENDSWITCH label (where all breaks jump to)
	asm = append(asm, switchInfo.EndLabel)

	return asm, nil
}

func (c *EndSwitchCommand) GetClass() compiler.CmdClass { return compiler.CmdFlowCtrl }
func (c *EndSwitchCommand) GetName() string              { return "ENDSWITCH" }
