import common
import paraview.simple as pw  # type: ignore


def run_normal(single_view=False):
    """Set up everything for the user to see when he opens ParaView.

    Args:
        single_view: Only set up a single view for PDF picture or video exprot.
    """

    common.load_plugins()

    state_file = common.find_state_file()
    if state_file:
        pw.LoadState(state_file)
        return

    # render_view_1 will not exist by default if pvbatch is launched.
    render_view_1 = pw.GetActiveViewOrCreate("RenderView")

    simulation_data = common.load_ensight_file(common.RES_FILE)
    # Go to the desired time step now, because the data fields will be scaled to it.
    common.goto_last_timestep()

    blocks = common.extract_default_blocks(simulation_data, render_view_1)
    # See the extract_default_blocks docstring for information.
    # Most functions in the common module have type hints, your IDE should recognize them.

    if common.CENOS_APP == "ANTENNAS":
        import functions_antennas as antennas

        ant_outline, ant_size, ant_offset = antennas.extract_antenna(blocks)

        render_view_2 = common.create_secondary_view()
        pw.AddCameraLink(render_view_1, render_view_2, "CameraLink1")
        antennas.show_dielectric(blocks, render_view_2)
        ant_outline.show_in_view(render_view_2)
        pw.UpdateScalarBars(view=render_view_2)

        ff_reader, ff_block = antennas.create_farfield(
            render_view_1, ant_size, ant_offset
        )

        if not single_view:
            # For some reason this has to be before create_polar_graphs,
            # otherwise it throws pythonBasedCsv reader exception.
            antennas.change_time_label(render_view_1)
            antennas.create_polar_graphs(render_view_1, ff_reader, ff_block, ant_offset)

            antennas.fix_single_timestep(simulation_data)
            antennas.add_tabs_hint()

    if not single_view:
        if common.IS_AXISYMMETRIC and common.CENOS_APP != "ANTENNAS":
            render_view_2 = common.create_secondary_view()
            common.create_axisymmetric_view(blocks, render_view_1, render_view_2)

    pw.LoadPalette(paletteName="WhiteBackground")
    common.add_case_name(render_view_1)
    common.format_fields(render_view_1)
    common.add_lighting(render_view_1)
    common.set_camera_position(render_view_1)
    pw.UpdateScalarBars(view=render_view_1)
    pw.SetActiveSource(simulation_data)
    pw.SetActiveView(render_view_1)

    if not single_view:
        import charts

        charts.create_charts(render_view_1, common.VIZ_CONF, common.RESULTS_JSON)
    pw.RenderAllViews()

    if single_view:
        return render_view_1


def run_pdf():
    run_normal(single_view=True)
    # This will use the existing renderview setup as the thumbnail.
    if common.CENOS_APP == "INDUCTION":
        from report.induction_report import main
    elif common.CENOS_APP == "ANTENNAS":
        from report.antennas_report import main
    else:
        raise RuntimeError(f"PDF reports are not configured for {common.CENOS_APP}")


def run_video():
    import video
    render_view_1 = run_normal(single_view=True)
    video.generate_video(render_view_1)
