local heliosphereTransforms = asset.require("scene/solarsystem/sun/transforms_heliosphere")
local propertyHelper = asset.require("util/property_helper")
local rot = asset.require("./carrington_to_heeq_rotation")



local fieldlinesDirectory = asset.syncedResource({
  Name = "Bastille Day MAS Fieldlines",
  Type = "HttpSynchronization",
  Identifier = "bastille_day_mas_fieldlines",
  Version = 1
})


local SunRadius = 695700000

-- Fieldlies from binaries
local Fieldlines = {
  Identifier = "MAS-MHD-Fieldlines-bastille-day-2000",
  Parent = heliosphereTransforms.HEEQ180ReferenceFrame.Identifier,
  -- TODO Elon: 21 April 2022. Interaction sphere should not depend on the transform scale.
  -- InteractionSphere = sunAsset.Sun.Renderable.Radii[1] * 1.05,
  InteractionSphere = 1 / SunRadius,
  Transform = {
    Rotation = rot.CarringtonLongitudeToHEEQ180Rotation,
    Scale = {
      Type = "StaticScale",
      Scale = SunRadius
    }
  },
  Renderable = {
    Type = "RenderableFieldlinesSequence",
    SourceFolder = fieldlinesDirectory,
    AlphaBlendlingEnabled = false,
    InputFileType = "Osfls",
    ColorTablePaths = {
      asset.localResource("transferfunctions/density-fieldlines.txt"),
      asset.localResource("transferfunctions/velocity-fieldlines.txt")
    },
    ColorTableRanges = {
      { 0, 1000000 },
      { 100, 2000 }
    },
    SimulationModel = "mas"
  },
  GUI = {
    Path = "/Solar System/Heliosphere/Bastille Day 2000",
    Name = "MAS Fieldlines",
    Description = [[Magnetic fieldlines for the bastille day CME event. This asset
      contains data from 2000-07-14 08:33 to 2000-07-14 11:49]]
  }
}

local ToggleFieldlines = {
  Identifier = "os.bastilleday.fieldlines.ToggleFieldlines",
  Name = "Toggle fieldlines",
  Command = [[
    if openspace.getPropertyValue("Scene.MAS-MHD-Fieldlines-bastille-day-2000.Renderable.Enabled") then
      openspace.setPropertyValueSingle(
        "Scene.MAS-MHD-Fieldlines-bastille-day-2000.Renderable.Fade",
        0.0,
        openspace.getPropertyValue("OpenSpaceEngine.FadeDuration"),
        "Linear",
        'openspace.setPropertyValueSingle("Scene.MAS-MHD-Fieldlines-bastille-day-2000.Renderable.Enabled", false)'
      )
    else
      openspace.setPropertyValueSingle("Scene.MAS-MHD-Fieldlines-bastille-day-2000.Renderable.Enabled", true)
      openspace.setPropertyValueSingle(
        "Scene.MAS-MHD-Fieldlines-bastille-day-2000.Renderable.Fade",
        1.0,
        openspace.getPropertyValue("OpenSpaceEngine.FadeDuration"),
        "Linear"
      )
    end
  ]],
  Documentation = "Toggle fieldline rendering of CME",
  GuiPath = "/Bastille-Day 2000",
  IsLocal = false
}


asset.onInitialize(function()
  openspace.action.registerAction(ToggleFieldlines)
  openspace.addSceneGraphNode(Fieldlines)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(Fieldlines)
  openspace.action.removeAction(ToggleFieldlines)
end)

asset.export(Fieldlines)
asset.export("ToggleFieldlines", ToggleFieldlines.Identifier)



asset.meta = {
  Name = "Predictive Science Inc. Fieldlines Bastille Day",
  Version = "1.1",
  Description = "Magnetic fieldlines for the bastille day CME event",
  Author = "CCMC, Jonathan Grangien, Matthias Berg",
  URL = "https://dx.doi.org/10.3847/1538-4357/aab36d",
  License = "CC-BY"
}
