local mod_name = minetest.get_current_modname()
local mod_path = minetest.get_modpath(mod_name)
local S = minetest.get_translator(mod_name)


local function on_load(itemstack, user)
    minetest.sound_play(("pmb_muskets_pistol_cocked"), {
        gain = 0.15,
        pos = user:get_pos(),
        object = user,
        pitch = (math.random()*0.1) + 0.90
    })
end

local function use_musket(itemstack, user, pointed_thing)
    if not pmb_muskets.on_fire(itemstack, user, pointed_thing) then
        return itemstack
    end
    if not user:is_player() then return itemstack end

    if itemstack:get_wear() > 0 then
        minetest.sound_play(("pmb_muskets_pistol_empty"), {
            gain = 1.5,
            pos = user:get_pos(),
            object = user,
            pitch = (math.random()*0.1) + 0.90
        })
        pmb_muskets.start_reload(user)
        return itemstack
    end

    itemstack:set_name("pmb_muskets:rifle")
    itemstack:set_wear(65536 - 1)

    local dir = user:get_look_dir()
    if true then
        local d = 0.005
        dir = vector.offset(dir, (math.random()*d)-(d/2), (math.random()*d)-(d/2), (math.random()*d)-(d/2))
    end
    local pos = user:get_pos()

    local ct = player_info and player_info.get(user)

    pos = pmb_muskets.get_eyepos(user)
    -- pos = vector.add(pos, dir)
    local yawoffset = minetest.yaw_to_dir(user:get_look_horizontal() - 1)
    yawoffset = vector.multiply(yawoffset, 0.2)
    local bullet_pos = pos --vector.add(pos, yawoffset)
    local ent = minetest.add_entity(bullet_pos, "pmb_muskets:musket_shot_ENTITY"):get_luaentity()
    ent._shooter = user
    ent._damage = {pierce = 20, projectile = 1}
    ent._dir = dir
    ent._speed = 300
    ent._bullet_drop = -20
    ent._lifetime = 1.5
    -- ent._last_pos = pos
    minetest.sound_play(("pmb_muskets_pistol"), {
        gain = 0.5,
        -- pos = object:get_pos(),
        object = user,
        max_hear_distance = 200,
        pitch = (math.random()*0.1) + 0.60
    })
    minetest.sound_play(("pmb_muskets_pistol"), {
        gain = 1.5,
        pos = bullet_pos,
        object = user,
        max_hear_distance = 100,
        pitch = (math.random()*0.1) + 1.9
    })

    local particle_pos = vector.add(pos, vector.multiply(dir, 2.4))
    particle_pos = vector.add(particle_pos, yawoffset)
    pmb_muskets.do_smoke_particles(particle_pos, 20, dir, 6, 0.1)
    pmb_muskets.do_particles(particle_pos, 10)
    local flashnode = minetest.get_node(bullet_pos)
    if flashnode.name == "air" then
        minetest.set_node(bullet_pos, {name="pmb_util:light_node_14"})
        minetest.get_node_timer(bullet_pos):set(1,0.8)
    end
    return itemstack
end

minetest.register_tool("pmb_muskets:rifle", {
    description = pmb_util.desc(S("Rifle"), "blue"),
    _tt_long_desc = S("Flintlock rifle. Slow to reload but very accurate and powerful."),
    _tt_how_to_use = S("Fire: [place node]").."\n"..S("Reload: 4-8s"),
    _tt_uses = "Infinite",
    inventory_image = "pmb_muskets_rifle_loading.png",
    wield_image = "pmb_muskets_rifle.png^[transform3",
    wield_scale = {x=8.0/3,y=5.0/3,z=4.0},
    tool_capabilities = {
        damage_groups = {
            blunt=2,
        },
    },
    groups = { musket = 1 },
    on_secondary_use = use_musket,
    on_place = use_musket,
    _wield3d_textures = "pmb_muskets:rifle_loaded",
    _wield3d_offset =   {x=-0.7, y=2.0, z=-2.3},
    _wield3d_scale = {x=0.12, y=0.12, z=0.12},
    _wield3d_rotation = {x=-90, y=45+12, z=-90},
    _override_item_entity = {
        visual_size = {x=0.12, y=0.12, z=0.12},
    },
})
local on_fire = function(itemstack, player)
    local ct = player_info.get(player)
    if ct and not ct.ctrl.sneak then
        return use_musket(itemstack, player)
    end
end
minetest.register_tool("pmb_muskets:rifle_loaded", {
    description = pmb_util.desc(S("Rifle"), "blue"),
    _tt_long_desc = S("Flintlock rifle. Slow to reload but very accurate and powerful."),
    _tt_how_to_use = S("Fire: [place node]").."\n"..S("Reload: 2-4s"),
    _tt_uses = "Infinite",
    inventory_image = "pmb_muskets_rifle.png",
    wield_image = "pmb_muskets_rifle_wield.png",
    wield_scale = {x=8.0*2.5,y=5.0*2.5,z=6.0},
    tool_capabilities = {
        damage_groups = {
            blunt=2,
        },
    },
    groups = { musket = 1 },
    -- on_use = use_musket,
    on_secondary_use = on_fire,
    on_place = on_fire,
    _wield3d_offset =   {x=-0.7, y=2.0, z=-2.3},
    _wield3d_scale = {x=0.12, y=0.12, z=0.12},
    _wield3d_rotation = {x=-90, y=45-12, z=-90},
    _override_item_entity = {
        visual_size = {x=0.12, y=0.12, z=0.12},
    },
})
if pmb_vars.technology_level >= 4 then
    local g = "pmb_muskets:pistol_loaded"
    local b = "pmb_items:iron_bar"
    minetest.register_craft({
        output = "pmb_muskets:rifle_loaded",
        recipe = {
        {g, b, b},
        },
    })
    local s = "pmb_muskets:rifle"
    minetest.register_craft({
        output = "pmb_muskets:rifle_loaded",
        recipe = {
        {s},
        },
    })
end

pmb_muskets.register_musket_reloader({
    name = "pmb_muskets:rifle",
    time = 5,
    loaded = "pmb_muskets:rifle_loaded",
    on_load = on_load,
    on_step = function(player, dtime, itemstack)
        return pmb_muskets.on_step_quick_reload(player, dtime, itemstack, {min=7, max=10})
    end,
    require_wield = true,
    sneak_boost = 1.0,
    sprint_boost = 1.0,
    movement_speed = 0.4,
    auto_reload = false,
})

pmb_util.register_on_wield({
    name = "pmb_muskets:rifle_loaded",
    on_change_to_item = function(player)
        pmb_util.unset_fov(player, "pmb_muskets:rifle_zoom")
        pmb_muskets.start_reload(player)
        player_model.set_anim(player, {tag="pmb_muskets_aim", actions={"aim"}})
    end,
    on_change_from_item = function(player)
        pmb_util.unset_fov(player, "pmb_muskets:rifle_zoom")
        player_model.unset_anim(player, "pmb_muskets_aim")
    end,
    on_step = function(player, dtime, itemstack)
        local ct = player_info.get(player)
        if ct and ct.ctrl.sneak and ct.ctrl.place then
            pmb_util.set_fov(player, {
                tag = "pmb_muskets:rifle_zoom",
                fov = 0.4,
                is_multiplier = true,
                transition_time = 0.2 })
        elseif pmb_util.has_fov(player, "pmb_muskets:rifle_zoom") ~= nil then
            pmb_util.unset_fov(player, "pmb_muskets:rifle_zoom")
        end
        if ct and ct.just_released.place and ct.ctrl.sneak then
            player:set_wielded_item(use_musket(player:get_wielded_item(), player))
        end
    end,
})

pmb_util.register_on_wield({
    name = "pmb_muskets:rifle",
    on_change_to_item = function(player)
        player_model.set_anim(player, {tag="pmb_muskets_reload", actions={"aim"}})
    end,
    on_change_from_item = function(player)
        player_model.unset_anim(player, "pmb_muskets_reload")
    end,
    on_step = function(player, dtime, itemstack)
        if itemstack:get_wear() < 65536 - 10 then
            player_model.set_anim(player, {tag="pmb_muskets_reload", actions={"sneak", "aim"}})
        end
    end,
})
