#########################################################################
##                                                                     ##
##  I N T E R S T E L L A R   C O N S O L E                            ##
##            Copyright (C) 2025- Misato Observatory, Wakayama, Japan  ##
##                                                                     ##
##  This software is available under the GPL (General Public License)  ##
##                                                                     ##
#########################################################################

# USAGE:
# $ tclsh create_star-labels_csv.tcl > tmp_work_star-labels.utf-8.txt

#########################################################################

#
# Procedures for handling OS-dependent processes.
#
source system-proc.tcl

#
# Import base proc
#
source -encoding $TCLSRC_ENCODING base-proc.tcl

set stellarium_direct_url "$stellarium_url/api/scripts/direct"
set stellarium_run_url "$stellarium_url/api/scripts/run"
set stellarium_status_url "$stellarium_url/api/main/status"

proc output_csv { lang list_obj } {

  global stellarium_direct_url
  global stellarium_status_url

  set str_code core.setAppLanguage('$lang')
  do_http_post $stellarium_direct_url "code=$str_code" ret buffer_codefield

  set out_csv ""

  foreach obj $list_obj {

    set str_code "core.selectObjectByName('$obj',true)"
    do_http_post $stellarium_direct_url "code=$str_code" ret buffer_codefield

    after 200

    set hr_str ""
    set list_replace {"&alpha;" "Aplha" "&delta;" "Delta" "&phi;" "Phi"}
    set info_str [get_object_data_from_stellarium $stellarium_status_url $hr_str $list_replace 1 ret_msg]

    set lines [split $info_str "\n"]
    set len_lines [llength $lines]
    for {set i 0} {$i < $len_lines} {incr i} {
      set txt_line [string trim [lindex $lines $i]]
      set status [regexp -- {^([^-()]+)} $txt_line match s2]
      if { $status == 1 } {
        set out_str [string trim $s2]
        if { $out_csv ne "" } {
          append out_csv ", "
        }
        append out_csv [format "%c%s%c" 34 $out_str 34]
        break
      }
    }

  }

  return $out_csv
  #return [encoding convertto utf-8 $out_csv]

}

set list_obj1 {"Sun" "Moon" "Mercury" "Venus" "Earth" "Mars" "Jupiter" "Saturn" "Uranus" "Neptune" "Pluto"}
set list_obj2 {"Polaris" "HIP65378A" "Alcor" "M45" "Spica" "Arcturus" "Regulus" "Denebola" "HIP63125A" "Vega" "Altair" "Deneb" "Albireo"  "Antares" "Fomalhaut" "HIP3419" "HIP677" "HIP10826" "Betelgeuse" "Rigel" "Sirius" "Aldebaran" "Pollux" "Castor" "Capella" "Procyon" "Canopus" "HIP71683" "Acrux"}

set list_langs {"ja_JP" "en_US" "fr_FR" "es_ES" "it_IT" "pt_PT" "de_DE" "ru_RU" "zh_TW"}

fconfigure stdout -translation binary

puts "######## Output is UTF-8 ########"

foreach lang $list_langs {

  puts "#### LANG = $lang"
  puts -nonewline stdout [output_csv $lang $list_obj1]
  puts ""
  puts -nonewline stdout [output_csv $lang $list_obj2]
  puts ""

}

