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



local TexturesPathEquatorial = asset.syncedResource({
Type = "HttpSynchronization",
  Name = "cutplanes_textures",
  Identifier = "cutplanes_textures",
  Version = 1
})

local TexturesPathMeridial = asset.syncedResource({
  Type = "HttpSynchronization",
  Name = "cutplane_meridial_textures",
  Identifier = "cutplane_meridial_textures",
  Version = 1
})


local EquatorialCutplane = {
  Identifier = "EquatorialCutplane-bastille-day-2000",
  Parent = transforms.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 = 695700000.0,
  Transform = {
    Rotation = rot.CarringtonLongitudeToHEEQ180Rotation
  },
  Renderable = {
    Type = "RenderablePlaneTimeVaryingImage",
    Size = 157000000000,
    Enabled = true,
    SourceFolder = TexturesPathEquatorial,
    BlendMode = "Normal",
    MirrorBackside = false,
    Opacity = 0.7
  },
  GUI = {
    Name = "Cutplane Equitorial",
    Path = "/Solar System/Heliosphere/Bastille Day 2000",
    Description = [[Equatorial cutplane sequence for the bastille day CME event. This
      asset contains data from 2000-07-14 08:38 to 2000-07-14 12:00]]
  }
}

local MeridialCutplane = {
  Identifier = "MeridialCutplane-bastille-day-2000",
  Parent = transforms.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 = 695700000,
  Transform = {
    Rotation = {
      Type = "StaticRotation",
      Rotation = { -math.pi/2, -math.pi, 0.0 }
    }
  },
  Renderable = {
    Type = "RenderablePlaneTimeVaryingImage",
    Size = 157000000000,
    Enabled = true,
    SourceFolder = TexturesPathMeridial,
    BlendMode = "Normal",
    MirrorBackside = false,
    Opacity = 0.7
  },
  GUI = {
    Name = "Cutplane Meridial",
    Path = "/Solar System/Heliosphere/Bastille Day 2000",
    Description = [[Meridial cutplane sequence for the bastille day CME event. This asset
      contains data from 2000-07-14 08:38 to 2000-07-14 12:00]]
  }
}

local ToggleEquatorial = {
  Identifier = "os.bastilleday.fluxnodescutplane.ToggleEquatorial",
  Name = "Toggle equatorial cutplane",
  Command = [[
    if openspace.getPropertyValue("Scene.EquatorialCutplane-bastille-day-2000.Renderable.Enabled") then
      openspace.setPropertyValueSingle(
        "Scene.EquatorialCutplane-bastille-day-2000.Renderable.Fade",
        0.0,
        openspace.getPropertyValue("OpenSpaceEngine.FadeDuration"),
        "Linear",
        'openspace.setPropertyValueSingle("Scene.EquatorialCutplane-bastille-day-2000.Renderable.Enabled", false)'
      )
    else
      openspace.setPropertyValueSingle("Scene.EquatorialCutplane-bastille-day-2000.Renderable.Enabled", true)
      openspace.setPropertyValueSingle(
        "Scene.EquatorialCutplane-bastille-day-2000.Renderable.Fade",
        1.0,
        openspace.getPropertyValue("OpenSpaceEngine.FadeDuration"),
        "Linear"
      )
    end
  ]],
  Documentation = "Toggle equatorial cutplane of CME",
  GuiPath = "/Bastille-Day 2000",
  IsLocal = false
}
local ToggleMeridial = {
  Identifier = "os.bastilleday.fluxnodescutplane.ToggleMeridial",
  Name = "Toggle meridial cutplane",
  Command = [[
    if openspace.getPropertyValue("Scene.MeridialCutplane-bastille-day-2000.Renderable.Enabled") then
      openspace.setPropertyValueSingle(
        "Scene.MeridialCutplane-bastille-day-2000.Renderable.Fade",
        0.0,
        openspace.getPropertyValue("OpenSpaceEngine.FadeDuration"),
        "Linear",
        'openspace.setPropertyValueSingle("Scene.MeridialCutplane-bastille-day-2000.Renderable.Enabled", false)'
      )
    else
      openspace.setPropertyValueSingle("Scene.MeridialCutplane-bastille-day-2000.Renderable.Enabled", true)
      openspace.setPropertyValueSingle(
        "Scene.MeridialCutplane-bastille-day-2000.Renderable.Fade",
        1.0,
        openspace.getPropertyValue("OpenSpaceEngine.FadeDuration"),
        "Linear"
      )
    end
  ]],
  Documentation = "Toggle meridial cutplane of CME",
  GuiPath = "/Bastille-Day 2000",
  IsLocal = false
}


asset.onInitialize(function()
  openspace.action.registerAction(ToggleEquatorial)
  openspace.action.registerAction(ToggleMeridial)
  openspace.addSceneGraphNode(EquatorialCutplane)
  openspace.addSceneGraphNode(MeridialCutplane)
end)

asset.onDeinitialize(function()
  openspace.removeSceneGraphNode(MeridialCutplane)
  openspace.removeSceneGraphNode(EquatorialCutplane)
  openspace.action.removeAction(ToggleEquatorial)
  openspace.action.removeAction(ToggleMeridial)
end)

asset.export("ToggleEquatorial", ToggleEquatorial.Identifier)
asset.export("ToggleMeridial", ToggleMeridial.Identifier)
asset.export(EquatorialCutplane)
asset.export(MeridialCutplane)



asset.meta = {
  Name = "Predictive Science Inc. Cutplanes Bastille Days",
  Version = "1.1",
  Description = "Cutplanes for the bastille day CME event",
  Author = "CCMC, Christian Adamsson, Emilie Ho",
  URL = "https://dx.doi.org/10.3847/1538-4357/aab36d",
  License = "CC-BY"
}
