#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

require("csv")
require("curses")
include Curses

def random(u,d)
  srand()
  a = rand(u - d) + d
  return a 
end
  
def loding
  init_screen
  begin
    curs_set(0)
    lord = 0
    srand()
    loop = random(2,1)
    while not lord == loop
      setpos(lines - 1,cols - 9)
      addstr"loding"
      refresh
      dot = 0
      lord += 1
      sleep(0.6)
      while not dot == 3
        addch"."
        refresh
        dot += 1
        sleep(0.5)
      end
      del = 0
      while not del == 9
        setpos(lines - 1,cols - 9)
        delch
        del += 1
      end
    end
  ensure
    close_screen
  end
  puts""
end

date = CSV.read("j2135_intro_1.csv", headers: true)  

output = spawn "display -geometry +0+0 j2135_intro_2.png"
puts"\e[1;4;34m佐藤隆之助(j2135)の自己紹介\e[m"
while 1
  puts" \e[1m>-=メニュー=-<\e[m"
  puts"\e[1;4m1\e[m.自己紹介"
  puts"\e[1;4m2\e[m.URL"
  puts"\e[1;4m3\e[m.終わる"
  print"\e[1;32m選択\e[0;1m…\e[1;33m"
  input = gets.to_i
  print"\e[m"
  if input == 1
    loding
    x = 0
    puts" \e[1m>-=自己紹介=-<\e[m"
    puts" =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
    while not x == 6
      printf(" |%s|%s|\n",date[x]["項目"],date[x]["内容"])
      print" =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
      gets
      x += 1
    end
  elsif input == 2
    loding
    x = 0
    puts"  \e[1m>-=URL=-<\e[m"
    puts" =-=-=-=-=-=-=-=-=-=-=-=-=-=-="
    while not x == 4
      x += 1
      printf(" |%d|%s |\n",x,date[x - 1]["解説"])
      puts" =-=-=-=-=-=-=-=-=-=-=-=-=-=-="
    end
    print"\e[1;32m選択\e[0;1m…\e[1;33m"
    input = gets.to_i
    print"\e[m"
    system("firefox "+date[input]['url']+"&")
  elsif input == 3
    break
  else
    print" \e[1;4;31m1/2/3で選んで下さい｡\e[m"
    gets
    redo
  end
end
Process.kill(:INT,output)
