
# Tas
# Written in ruby
#
# start.rb
# runned as program's startup
#
# licenced under MIT Licence



require 'rubygems'
require 'bundler/setup'
require "highline/import"
require "octokit"

def header_login
	puts "\e[H\e[2J+--------------+"
	puts "I     TAS      I"
	puts "I GitHub Login I"
	puts "+--------------+"
end

def header_client
	puts "\e[H\e[2J+---------------+"
	puts "I      TAS      I"
	puts "I GitHub Client I"
	puts "+---------------+"
end

def logout(user)
	header_client()
	puts("Logging out...")
	user.logout
	return nil
end


def run
	while true do
		header_login()

		username = ask("Username: ") 
		break if username == "exit"
		password = ask("Password: ") { |q| q.echo = "*" }

		client = Octokit::Client.new \
			:login    => username,
			:password => password

		header_login()
		puts "Logging in..."

		begin
			user = client.user
		rescue
			header_login()
			say("<%= color('Login failed :(', RED) %>")
			sleep 3
			next
		end
		header_login()
		say("<%= color('Login OK :)', GREEN) %>")
		sleep 3
		header_client()
		puts "\nHello, #{user.name}."
		sleep 1
		while user do
			header_client()
			choose do |menu|
				menu.header = "What you how to do?  "
				menu.choice(:Logout) { 
					choices = "yn"
					answer = ask("\e[H\e[2JConfirm? [#{choices}]? ") do |q|
						q.echo      = false
						q.character = true
						q.validate  = /\A[#{choices}]\Z/
					end
					say "\e[H\e[2JConfirm? <%= color('YES', BLUE) %>" if answer == "y"
					say "\e[H\e[2JConfirm? <%= color('NO', BLUE) %>" if answer == "n"
					sleep(1)
					user = logout(user) if answer == "y"
				}
				menu.choice(:Details) {
					header_client()
					say("Username: #{user.login}")
					say("Real name: #{user.name}") if user.name != nil
					say("Location: #{user.location}") if user.location != nil
					say("Email: #{user.email}") if user.email != nil
					say("Company: #{user.company}") if user.company != nil
					say("Press ENTER to continue.")
					gets
				}
			end
		end
	end
end