local man = {}
local t = require('spec.zync_test')

man.conf_name = 'zconf.lua'
man.conf_cont = [[
return {
    board = '%s',
    runner = '%s',

    -- Uncomment to add additional args
    -- args = {
    --     -- run = '',
    -- },

    -- Uncomment to configure build profiles
    -- profiles = {
    --     sim = {
    --         board = 'native_sim',
    --     },
    --     prod = {
    --         -- runner = '',
    --     }
    -- }
}]]
man.board = 'nucleo_f767zi'
man.runner = 'openocd'
man.zephyr_base = 'test/zephyr'
man.tconf = {
    board = 'test_board',
    runner = 'test_runner',
}

man.clean = {
    [1] = {
        path = 't1',
        post = true,
    },

    [2] = {
        path = 'build',
        post = true,
    },

    [3] = {
        path = 'build/a',
        post = false,
    },

    [4] = {
        path = 'build/b',
        post = false,
    },
}

man.init = {
    [1] = {
        board = nil,
        runner = nil,
    },

    [2] = {
        board = nil,
        runner = 'test_runner',
    },

    [3] = {
        board = 'test_board',
        runner = nil,
    },

    [4] = {
        board = 'test_board',
        runner = 'test_runner',
    },
}

man.conf = {
    [1] = {
        board = nil,
        runner = nil,
    },

    [2] = {
        board = nil,
        runner = 'test_runner',
    },

    [3] = {
        board = 'test_board',
        runner = nil,
    },

    [4] = {
        board = 'test_board',
        runner = 'test_runner',
    },

    [5] = {
        board = nil,
        runner = nil,
        sysbuild = true,
    },

    [6] = {
        board = nil,
        runner = 'test_runner',
        sysbuild = true,
    },

    [7] = {
        board = 'test_board',
        runner = nil,
        sysbuild = true,
    },

    [8] = {
        board = 'test_board',
        runner = 'test_runner',
        sysbuild = true,
    },
}

function man.build_conf(tc)
    local conf = tc
    conf.zephyr_base = man.zephyr_base
    return conf
end

function man.build_cmd(config)
    local cmd = { 'cmake -GNinja' }
    local project_dir = config.project_dir or '..'

    if config.board then
        table.insert(cmd, '-DBOARD=' .. config.board)
    end

    if config.runner then
        table.insert(cmd, '-DBOARD_FLASH_RUNNER=' .. config.runner)
    end

    if config.sysbuild and config.sysbuild == true then
        table.insert(cmd, '-DAPP_DIR=' .. project_dir)
        table.insert(cmd, man.zephyr_base .. '/share/sysbuild')
    else
        table.insert(cmd, project_dir)
    end

    return cmd
end

function man.gen_cache(board)
    os.execute('rm CMakeCache.txt > /dev/null 2>&1')
    t.helpers.write_file('CMakeCache.txt', 'BOARD:STRING=' .. board .. ' ')
end

function man.gen_build()
    for _, dir in ipairs(man.clean) do
        t.lfs.mkdir(dir.path)
        t.helpers.dir_exists(dir.path, true)
    end
end

function man.check_build()
    for _, dir in ipairs(man.clean) do
        t.helpers.dir_exists(dir.path, dir.post)
    end
end

function man.create_config(board)
    man.gen_cache(board)
    os.execute('touch build.ninja')
end

return man
