-- Flutter-specific Neovim configuration
-- Keymaps for Flutter development workflow

local function flutter_root()
    return vim.fn.finddir("android", vim.fn.getcwd() .. ";") ~= ""
        or vim.fn.findfile("pubspec.yaml", vim.fn.getcwd() .. ";") ~= ""
end

-- Create Flutter-specific user commands
vim.api.nvim_create_user_command("FlutterRun", function()
    vim.cmd("split | terminal flutter run")
end, {})

vim.api.nvim_create_user_command("FlutterClean", function()
    vim.cmd("terminal flutter clean")
end, {})

vim.api.nvim_create_user_command("FlutterPubGet", function()
    vim.cmd("terminal flutter pub get")
end, {})

vim.api.nvim_create_user_command("FlutterPubOutdated", function()
    vim.cmd("terminal flutter pub outdated")
end, {})

vim.api.nvim_create_user_command("FlutterPubUpgrade", function()
    vim.cmd("terminal flutter pub upgrade")
end, {})

vim.api.nvim_create_user_command("FlutterAnalyze", function()
    vim.cmd("terminal flutter analyze")
end, {})

vim.api.nvim_create_user_command("FlutterTest", function()
    vim.cmd("terminal flutter test")
end, {})

vim.api.nvim_create_user_command("FlutterBuildApk", function()
    vim.cmd("terminal flutter build apk")
end, {})

vim.api.nvim_create_user_command("FlutterDevices", function()
    vim.cmd("terminal flutter devices")
end, {})

-- Dart filetype-specific settings
vim.api.nvim_create_autocmd("FileType", {
    pattern = "dart",
    callback = function()
        vim.opt_local.expandtab = true
        vim.opt_local.tabstop = 2
        vim.opt_local.shiftwidth = 2
        vim.opt_local.softtabstop = 2
        vim.opt_local.commentstring = "// %s"
        vim.opt_local.colorcolumn = "80"
    end,
})

-- Open Flutter DevTools in browser
vim.api.nvim_set_keymap("n", "<leader>fd", ":!flutter devtools<CR>", { noremap = true, silent = false, desc = "Flutter: DevTools" })

-- Flutter Run in a new tmux split (if tmux available)
vim.api.nvim_set_keymap("n", "<leader>fe", ":terminal flutter emulators<CR>", { noremap = true, silent = false, desc = "Flutter: List Emulators" })
