local load_fixture = require "tests.load_fixture"
local tw = require 'treewalker'
local h = require 'tests.treewalker.helpers'
local lines = require 'treewalker.lines'

describe("In a C Sharp file", function()
  before_each(function()
    load_fixture("/c-sharp.cs")
  end)

  h.ensure_has_parser("c_sharp")

  it("moves around", function()
    vim.fn.cursor(7, 5)
    tw.move_down()
    h.assert_cursor_at(27, 5)
    tw.move_down()
    h.assert_cursor_at(50, 5)
    tw.move_up()
    h.assert_cursor_at(27, 5)
    tw.move_in()
    h.assert_cursor_at(29, 9)
    tw.move_down()
    h.assert_cursor_at(31, 9)
    tw.move_down()
    h.assert_cursor_at(36, 9)
    tw.move_down()
    h.assert_cursor_at(41, 9)
    tw.move_up()
    h.assert_cursor_at(36, 9)
    tw.move_up()
    h.assert_cursor_at(31, 9)
    tw.move_out()
    h.assert_cursor_at(27, 5)
  end)

  it("moves through foreach/try/catch blocks", function()
    vim.fn.cursor(113, 13)
    tw.move_down()
    h.assert_cursor_at(114, 13)
    tw.move_down()
    h.assert_cursor_at(116, 13)
    tw.move_down()
    h.assert_cursor_at(117, 13)
    tw.move_down()
    h.assert_cursor_at(122, 13)
    tw.move_down()
    h.assert_cursor_at(130, 13)
    tw.move_down()
    h.assert_cursor_at(135, 13)
    tw.move_down()
    h.assert_cursor_at(143, 13)
    tw.move_down()
    h.assert_cursor_at(144, 13)
  end)

  it("swaps down classes", function()
    vim.fn.cursor(7, 5)
    local first_block = lines.get_lines(6, 24)
    local second_block = lines.get_lines(26, 48)
    tw.swap_down()
    assert.same(second_block, lines.get_lines(6, 28))
    assert.same(first_block, lines.get_lines(30, 48))
    h.assert_cursor_at(31, 5)
  end)

  it("swaps down functions", function()
    vim.fn.cursor(31, 9)
    local first_block = lines.get_lines(31, 34)
    local second_block = lines.get_lines(36, 39)
    tw.swap_down()
    assert.same(second_block, lines.get_lines(31, 34))
    assert.same(first_block, lines.get_lines(36, 39))
    h.assert_cursor_at(36, 9)
  end)

  it("swaps up classes", function()
    vim.fn.cursor(27, 5)
    local first_block = lines.get_lines(6, 24)
    local second_block = lines.get_lines(26, 48)
    tw.swap_up()
    assert.same(second_block, lines.get_lines(6, 28))
    assert.same(first_block, lines.get_lines(30, 48))
    h.assert_cursor_at(7, 5)
  end)

  it("swaps up functions", function()
    vim.fn.cursor(36, 9)
    local first_block = lines.get_lines(31, 34)
    local second_block = lines.get_lines(36, 39)
    tw.swap_up()
    assert.same(second_block, lines.get_lines(31, 34))
    assert.same(first_block, lines.get_lines(36, 39))
    h.assert_cursor_at(31, 9)
  end)

  it("swaps inner blocks across functions", function()
    vim.fn.cursor(122, 13)
    local first_block = lines.get_lines(122, 125)
    local second_block = lines.get_lines(130, 138)
    tw.swap_down()
    assert.same(second_block, lines.get_lines(122, 130))
    assert.same(first_block, lines.get_lines(135, 138))
    h.assert_cursor_at(135, 13)
  end)
end)
