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

require"curses"
require"csv"

include Curses

def loding(ld,lu)
  printf("\e[0;1m")
  clear
  curs_set(0)
  lx = 0
  srand()
  ly = rand(ld..lu)
  while not lx == ly
    setpos(lines - 1,cols - 13)
    addstr"読み込み中"
    refresh
    ldot = 0
    lx += 1
    sleep(0.6)
    while not ldot == 3
      addch"."
      refresh
      ldot += 1
      sleep(0.5)
    end
    clear
    refresh
  end    
end


def cprint(cc,cy,cx,ct)
  printf("\e[m")
  printf(cc)
  setpos(cy,cols / 2 + cx)
  addstr(sprintf("%s",ct))
  refresh
end

init_screen
begin
  while 1
    clear
    curs_set(0)
    noecho
    stdscr.keypad(true)
    cprint("\e[0;1;21;33m",1,-8,"タイピング ゲーム")
    cprint("\e[0;1;31m",4,-2,"遊ぶ")
    cprint("\e[0;1;34m",6,-2,"設定")
    cprint("\e[0;1;32m",8,-2,"終了")
    x = cols / 2 - 4
    y = 4
    sentaku = 1
    while 1
      printf("\e[1;31m")
      setpos(y,x)
      addstr(">")
      refresh
      input = getch
      setpos(y,x)
      addch(" ")
      refresh
      case input
      when Curses::KEY_UP
        if y != 4
          y -= 2
          sentaku -= 1
        end
      when Curses::KEY_DOWN
        if y != 8
          y += 2
          sentaku += 1
        end
      when Curses::KEY_RIGHT
        break
      end
    end
    if sentaku == 1
      loding(1,2)
      while 1
        clear
        refresh
        cprint("\e[1;4;31m",1,-5,">-=遊ぶ=-<")
        cprint("\e[1;33m",4,-2,"初級  2~7文字")
        cprint("\e[1;33m",6,-2,"中級 5~10文字")
        cprint("\e[1;33m",8,-2,"上級 9~14文字")
        cprint("\e[1;32m",10,-2,"戻る")
        x = cols / 2 - 4
        y = 4
        sentaku = 1
        while 1
          printf("\e[1;31m")
          setpos(y,x)
          addch(">")
          input = getch
          setpos(y,x)
          addch(" ")
          refresh
          case input
          when Curses::KEY_UP
            if y != 4
              y -= 2
              sentaku -= 1
            end
          when Curses::KEY_DOWN
            if y != 10
              y += 2
              sentaku += 1
            end
          when Curses::KEY_RIGHT
            break
          end
        end
        clear
        if sentaku == 4
          break
        end        
        cprint("\e[1;37m",lines / 2,-14,"どれかのキーを押してスタート")
        getch
        clear
        if sentaku == 1
        elsif sentaku == 2
        elsif sentaku == 3
        end
      end
      loding(1,2)
      redo
    elsif sentaku == 2
      loding(1,1)
      while 1
        clear
        cprint("\e[1;4;34m",1,-5,">-=設定=-<")
        cprint("\e[1;32m",4,-2,"戻る")
        x = cols / 2 - 4
        y = 4
        sentaku = 1
        while 1
          print("\e[1;31m")
          setpos(y,x)
          addstr(">")
          refresh
          input = getch
          setpos(y,x)
          addch(" ")
          refresh
          case input
          when Curses::KEY_UP
            if y != 4
              y -= 2
              sentaku -= 1
            end
          when Curses::KEY_DOWN
            if y != 4
              y += 2
              sentaku += 1
            end
          when Curses::KEY_RIGHT
            break
          end
        end
        break
      end
      loding(1,1)
      redo
    elsif sentaku == 3
      clear
      refresh
      break
    end
  end
  clear
  ensure
  close_screen
end
