local vec2 = require "rheingold_powerline_vec2"
local vec3 = require "rheingold_powerline_vec2"
local polygon = require "rheingold_powerline_polygon"
local powerline = require "rheingold_powerline"

local modes = {"plan", "span", "reset"}
local tensions = { 0.00000, }

function data()
    return {
        type = "ASSET_DEFAULT",
        buildMode = "MULTI",
        categories = {"A_Oberleitungsmasten"},
        availability = {
            yearFrom = 0,
            yearTo = 0
        },
        description = {
            name = _("Oberleitungsspinne Planer"),
            description = _("Toolbox für Abspannmasten")
        },
        order = 5,
        skipCollision = true,
        autoRemovable = false,
        params = {
            {
                key = "rheingold_powerline_mode",
                name = _("rheingold_powerline_mode"),
                values = {_("plan"), _("span"), _("reset")},
                defaultIndex = 0
            },
                    
            {
                key = "rheingold_powerline_type",
                name = _("rheingold_powerline_type"),
                values = {_("Obere Leitung"), _("Untere Leitung") },
                defaultIndex = 0
            },

            {
                key = "rheingold_powerline_tension",
                name = _("rheingold_powerline_tension"),
                values = {_("Leitung")},
                defaultIndex = 1
            }
        },
        updateFn = function(params)
            local result = {
                models = {},
                cost = 10,
                bulldozeCost = 10,
                maintenanceCost = 0
            }
           
            local mode = modes[params.rheingold_powerline_mode + 1]
            local tension = tensions[params.rheingold_powerline_tension + 1]
            powerline.getObjects()
                        
            local markers = powerline.markerStore
            local finishers = powerline.finisherStore          
            local width = 4
            local model
            local configuration = {}


           if params.rheingold_powerline_type == 0 then
                -- Obere Leitung
                model = "rheingold_a_ausrichthilfe_7_7.mdl"                
                width = 24
                configuration = {
                    --Die Z und X Koordinaten wo die Kabel aufgespannt sind. Wir können hier auch mehrere Masttypen anbieten
                    --Eine Zeile pro Kabel, Format {Höhe, Abstand von Mitte}
                    {7.45, 0, 0.6},
			
                    
                }
            
            elseif params.rheingold_powerline_type == 1 then
                -- Untere Leitung
                model = "rheingold_a_ausrichthilfe_6_4.mdl"
                width = 11               
                configuration = {                    
                     {6.10, 0.0, 0.6},
                }
            
            end

            local points = powerline.getPoints(markers)
            local normals = powerline.getNormals(points)
            local outline = powerline.getOutline(points, normals, width)

            result.models = {}
            result.terrainAlignmentLists = {
                {
                    type = "EQUAL",
                    faces = {}
                }
            }

            if mode == "plan" then
                for i = 1, #finishers do
                    local finisher = finishers[i]
                    game.interface.bulldoze(finisher.id)
                end

                powerline.finisherStore = nil

                powerline.buildPylon({0, 0, 0}, {0, 1, 0}, model, result)

                for i = 1, #markers + 1 do
                    result.models[#result.models + 1] = {
                        id = "asset/rheingold_powerline_marker.mdl",
                        transf = {4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, 0, 0, 0, 0, 1}
                    }
                end

                if outline then
                    embankmentzone = {
                        polygon = outline,
                        draw = true,
                        drawColor = {221 / 255, 203 / 255, 149 / 255, 1.0}
                    }
                    game.interface.setZone("embankmentzone", embankmentzone)
                end
            elseif mode == "reset" then
                result.models[#result.models + 1] = {
                    id = "asset/rheingold_powerline_finisher.mdl",
                    transf = {0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1}
                }

                game.interface.setZone("embankmentzone", nil)

                for i = 1, #markers do
                    local marker = markers[i]
                    game.interface.bulldoze(marker.id)
                end

                powerline.markerStore = nil

            elseif mode == "span" then
                result.models[#result.models + 1] = {
                    id = "asset/rheingold_powerline_finisher.mdl",
                    transf = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
                }

                game.interface.setZone("embankmentzone", nil)

                for i = 1, #markers do
                    local marker = markers[i]
                    game.interface.bulldoze(marker.id)
                end

                powerline.markerStore = nil

                if (not points) or (#points < 2) or (polygon.isSelfIntersecting(outline)) then
                    return result
                end

                local player = game.interface.getPlayer()

                local slope = 0.0005
    
                for i = 1, #points do
                    local p = points[i]                  
                    
                    local pylon =
                    game.interface.buildConstruction(
                    "asset/rheingold_powerline_pylon.con",
                    {
                        point = points[i],
                        normal = normals[i],                       
                        model = model,                        
                    },
                    {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
                    )

                    game.interface.setPlayer(pylon, player)
                end
                
                for i = 1, #points - 1 do
                    local point1 = points[i]
                    local point2 = points[i + 1]
                    local normal1 = normals[i]
                    local normal2 = normals[i + 1]
    
                    local cable =
                    game.interface.buildConstruction(
                    "asset/rheingold_powerline_cable.con",
                    {
                        point1 = point1,
                        point2 = point2,
                        normal1 = normal1,
                        normal2 = normal2,
                        configuration = configuration,                        
                        tension = tension                       
                    },
                    {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
                    )

                    game.interface.setPlayer(cable, player)
                end                
            else
                error("powerline: invalid operation ", mode)
            end

            return result
        end
    }
end
