require "aircraft_gun_mounts"
require "scripts/database/Scripts/utils"

weapons_table.aircraft_guns = namespace();

function get_aircraft_guns_by_wstype(ws_type)
    for _, v in pairs(weapons_table.aircraft_guns) do
        if (v.ws_type == ws_type) then
            return v;
        end
    end
    return nil;
end

function get_aircraft_ammo_mass(guns)
    local res = 0;
    for _, v in pairs(guns.mounts) do
        res = res + v.supply.get_mass();
    end
    
    return res;
end

function get_aircraft_ammo_mass_by_wstype(ws_type)
    local g = get_aircraft_guns_by_wstype(ws_type);
    if (g == nil) then
        error(string.format("Guns not found for %d", ws_type));
    end
    
    return get_aircraft_ammo_mass(g);
end

function aircraft_guns(craft_name, ...)
    local type = _G[craft_name];
    if type == nil then
        error("Unknown type for "..craft_name);
    end
    
    local res = dbtype("wAircraftGunEquipment", {
        ws_type = type;
    });
    
    res.mounts = {};
    
    for i, v in ipairs(arg) do
        res.mounts[i] = v;
    end
    
    weapons_table.aircraft_guns[craft_name] = res
end

function gun_mount(template_name, ammo_override, mount_override)
    local template = gun_mount_templates[template_name];
    
    if template == nil then
        error("Unknown aircraft gun mount template: "..template_name);
    end
    
    local res = dbtype("wAircraftGunMount", {});

    res.supply_position = {0, 0, 0};

    copy_recursive_with_metatables(res, template, 2);
    copy_recursive(res.supply, ammo_override);  
    if mount_override ~= nil then
        copy_recursive(res, mount_override);    
    end
	
	if template.drop_cartridge ~= nil  and 
	   template.drop_cartridge == true then
	   res.drop_cartridge = 1 
	else
       res.drop_cartridge = 0
	end

    res.supply.get_mass = function () return template.supply.get_mass(res.supply) end        
    
    return res;
end

aircraft_guns("MiG_23", 
    gun_mount("GSh_23_2", { count = 260 })
);

aircraft_guns("MiG_29", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("MiG_29C", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("MiG_29G", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("MIG_29K", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("SU_17M4", 
    gun_mount("NR_30", { count = 80 }),
    gun_mount("NR_30", { count = 80 })
);

aircraft_guns("Su_27", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("Su_33", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("MiG_25");
aircraft_guns("MiG_25P");

aircraft_guns("MiG_31", 
    gun_mount("GSh_23_6", { count = 260 })
);

aircraft_guns("MiG_27", 
    gun_mount("GSh_30_6", { count = 320 })
);

aircraft_guns("Su_24", 
    gun_mount("GSh_23_6", { count = 500 })
);

aircraft_guns("Su_24MR", 
    gun_mount("GSh_23_6", { count = 500 })
);

aircraft_guns("Su_30", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("Su_25", 
    gun_mount("GSh_30_2", { count = 250 })
);

aircraft_guns("Su_25T", 
    gun_mount("GSh_30_2", { count = 250 })
);

aircraft_guns("Su_39", 
    gun_mount("GSh_30_2", { count = 250 })
);

aircraft_guns("Su_34", 
    gun_mount("GSh_30_1", { count = 150 })
);

aircraft_guns("Tu_22M3", 
    gun_mount("GSh_23_2", { count = 1200 })
);

aircraft_guns("Tu_95", 
    gun_mount("GSh_23_2", { count = 600 }),
    gun_mount("GSh_23_2", { count = 600 })
);

aircraft_guns("Tu_142", 
    gun_mount("GSh_23_2", { count = 600 }),
    gun_mount("GSh_23_2", { count = 600 })
);

aircraft_guns("IL_76", 
    gun_mount("GSh_23_2", { count = 600 }),
    gun_mount("GSh_23_2", { count = 600 })
);

aircraft_guns("Tu_160");
aircraft_guns("IL_78");
aircraft_guns("AN_26B");
aircraft_guns("AN_30M");
aircraft_guns("Tu_143");
aircraft_guns("Tu_141");
aircraft_guns("Yak_40");


aircraft_guns("F_14", 
    gun_mount("M_61", { count = 675 })
);

aircraft_guns("F_15", 
    gun_mount("M_61", { count = 940 })
);

aircraft_guns("F_16", 
    gun_mount("M_61", { count = 511 })
);

aircraft_guns("F_16A", 
    gun_mount("M_61", { count = 511 })
);

aircraft_guns("FA_18", 
    gun_mount("M_61", { count = 570 })
);

aircraft_guns("FA_18C", 
    gun_mount("M_61", { count = 570 })
);

aircraft_guns("F_2", 
    gun_mount("BK_27", { count = 180 })
);

aircraft_guns("TORNADO_IDS", 
    gun_mount("BK_27", { count = 180 }),
    gun_mount("BK_27", { count = 180 })
);


aircraft_guns("A_10",
    gun_mount("GAU_8", { count = 1150 })
);
    
aircraft_guns("F_4E",
    gun_mount("M_61", { count = 640 })
);

aircraft_guns("F_5E",
    gun_mount("M_39", { count = 280 }),
    gun_mount("M_39", { count = 280 })
);

aircraft_guns("F_5E",
    gun_mount("M_39", { count = 280 }),
    gun_mount("M_39", { count = 280 })
);

aircraft_guns("Mirage",
    gun_mount("DEFA_554", { count = 125 }),
    gun_mount("DEFA_554", { count = 125 })
);

aircraft_guns("AV_8B",
    gun_mount("ADEN", { count = 200 }),
    gun_mount("ADEN", { count = 200 })
);

aircraft_guns("F_111");
aircraft_guns("F_117");
aircraft_guns("B_1");
aircraft_guns("B_52");
aircraft_guns("A_50");
aircraft_guns("E_3");
aircraft_guns("KC_10");
aircraft_guns("C_130");
aircraft_guns("E_2C");
aircraft_guns("S_3A");
aircraft_guns("S_3R");
aircraft_guns("RQ_1A_Predator");
aircraft_guns("EA_6B");
aircraft_guns("C_17");

aircraft_guns("MI_24W",
    gun_mount("YakB_12_7", { count = 1400 })
);

aircraft_guns("KA_50",
    gun_mount("2A42", 
        { count1 = 240, count2 = 220, second_box_offset = {0.909, 0, 0} }, 
        { supply_position = {0.305, -0.6, 0.0} } 
    )
);

aircraft_guns("KA_52",
    gun_mount("2A42", { count1 = 240, count2 = 220 })
);

aircraft_guns("MI_28N",
    gun_mount("2A42", { count1 = 240, count2 = 220 })
);

aircraft_guns("AH_64A",
    gun_mount("M_230", { count = 1200 })
);

aircraft_guns("AH_64D",
    gun_mount("M_230", { count = 1200 })
);

aircraft_guns("AH_1W",
    gun_mount("M_197", { count = 750 })
);

aircraft_guns("OH_58D")

aircraft_guns("Sea_Lynx")
aircraft_guns("MI_8MT")
aircraft_guns("MI_26")
aircraft_guns("KA_27")
aircraft_guns("CH_47D")
aircraft_guns("CH_53E")
aircraft_guns("SH_60B")
aircraft_guns("UH_60A")
aircraft_guns("SH_3H")
aircraft_guns("AB_212")