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

require"curses";include Curses
require"csv"

sentence    = CSV.read("typing-texts.csv" , :headers => true)#タイピング本文
sentence_utu= CSV.read("typing-texts-utu.csv",:headers => true)
menu_sentakusi ={1=>"play",2=>"option",3=>"practice"
                }
class Window
  @@numbers = CSV.read("futaketa-alpha.csv",:headers => true)
def putSentakusi(nani,zip = @@numbers.headers)
  for na,s in nani.zip(zip)
    self.addstr(sprintf("%s => %s\n",s,na))
  end
  self.refresh
end
def getAns()
  self.addstr("入力:")
  self.refresh
  while true
    a = getch 
    if a == "q"
      return "q"
    elsif @@numbers[a]
      return @@numbers[a].join.to_i
   end
  end
end
def addstr_sousyoku(nani,str)
  nani.each{|n|self.attron(n)}
               self.addstr(str)
  nani.each{|n|self.attroff(n)}        
end
end

def delete
  dy = 0
  while not dy == cols
    dx = 0
    while not dx == lines
      setpos(dy,dx)
      delch
      dx += 0.03125 / 2
    end
    dy += 1
  end
  refresh
end

def loding(ld,lu)
  printf("\e[0;1m")
  delete
  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
    delete
    refresh
  end    
end

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

def choice(cu,cd,cn)
  x = cols / 2 + cn
  y = 4
  sentaku = 1
  while 1
    printf("\e[1;37m")
    setpos(y,x)
    addstr(">")
    refresh
    input = getch
    setpos(y,x)
    addch(" ")
    refresh
    case input
    when Curses::KEY_UP
      if y != cu
        y -= 2
        sentaku -= 1
      end
    when Curses::KEY_DOWN
      if y != cd
        y += 2
        sentaku += 1
      end
    when Curses::KEY_RIGHT
      break
    end
  end
  delete
  return sentaku
end

sentaku = 0
init_screen
noecho
cbreak
begin
  while 1
    delete
    curs_set(0)
    stdscr.keypad(true)
    cprint("\e[1;21;33m",1,cols / 2 -5,"学習ゲーム")
    cprint("\e[91m",4,cols / 2 -2,"未定")
    cprint("\e[94m",6,cols / 2 -2,"計算")
    cprint("\e[92m",8,cols / 2 -5,"タイピング")
    cprint("\e[m",10,cols / 2 -2,"終了")
    home_sntk = choice(4,10,-7)
    if home_sntk == 4
      break
    end
    loding(1,2)
    if home_sntk == 1
      while 1
        delete
        refresh
        cprint("\e[1;4;31m",1,cols / 2 -5,">-=未定=-<")
        cprint("\e[33m",4,cols / 2 -2,"未定")
        cprint("\e[33m",6,cols / 2 -2,"未定")
        cprint("\e[33m",8,cols / 2 -2,"未定")
        cprint("\e[m",10,cols / 2 -2,"戻る")
        mitei_sntk = choice(4,10,-4)
        if mitei_sntk == 4
          break
        end        
        cprint("\e[1;37m",lines / 2,cols / 2 -14,"どれかのキーを押してスタート")
        getch
        delete
        if mitei_sntk == 1
        elsif mitei_sntk == 2
        elsif mitei_sntk == 3
        end
      end
      loding(1,2)
      redo
    elsif sentaku == 2
      while 1
        delete
        cprint("\e[1;4;34m",1,cols / 2 -5,">-=計算=-<")
        cprint("\e[34m",4,cols / 2 -7,"足し算・引き算")
        cprint("\e[34m",6,cols / 2 -7,"掛け算・割り算")
        cprint("\e[34m",8,cols / 2 -2,"設定")
        cprint("\e[m",10,cols / 2 -2,"戻る")
        keisan_sntk = choice(4,10,-9)
        delete
        if keisan_sntk == 5
          redo
        elsif keisan_sntk == 6
          break
        end 
      end
      loding(1,2)
      redo
    elsif home_sntk == 3
      while 1
        delete
        cprint("\e[1;4;32m",1,cols / 2 - 8,">-=タイピング=-<")
        cprint("\e[32m",4,cols / 2 - 3,"始める")
        cprint("\e[32m",6,cols / 2 -2,"設定")
        cprint("\e[m",8,cols / 2 - 2,"戻る")
        type_sntk = choice(4,8,-5)
        delete
        if type_sntk == 1
          while 1
              x = cols / 2 -5
              y = 4
              type_sntk_game = 1
              while 1
                printf("\e[1;37m")
                setpos(y,x)
                addstr(">")
                refresh
                input = getch
                setpos(y,x)
                addch(" ")
                refresh
                case input
                when Curses::KEY_UP
                  if y != 4
                    y -= 2
                    type_sntk_game -= 1
                  end
                when Curses::KEY_DOWN
                  if y != 14
                    y += 2
                    type_sntk_game += 1
                  end
                when Curses::KEY_RIGHT
                  if type_sntk_game == 
                  end
                end
              end
              delete
          end
        elsif type_sntk == 2
          redo
        elsif type_sntk == 3
          break
        end
      end
      loding(1,2)
    end
  end
  delete
  ensure
  close_screen
end
