require 'json'
require 'tk'

class FortuneTeller
  def initialize
    @fortunes = [
      "Tez orada hayotingizda kutilmagan hodisa sodir boladi!",
      "Boylik eshiklari ochiladi, lekin sabr qilish kerak.",
      "Dostlaringiz orasida yashirin dushman bor, ehtiyot boling!",
      "Orzuingiz tez orada royobga chiqadi!",
      "Kelasi oy sevgi hayotingizda kutilmagan ozgarish boladi.",
      "Ishingizda katta muvaffaqiyatlarga erishasiz!",
      "Bugun omad sizga kulib boqadi, imkoniyatni qoldan boy bermang!"
    ]
    
    @advice = [
      "Koproq suv iching va sogligingizga etibor bering!",
      "Bugun yangi narsalarni sinab korish uchun ajoyib kun!",
      "Muvaffaqiyatning siri  sabr-toqat va harakat!",
      "Bugun eski dostlaringizdan biriga yozing, ular sizni sogingan bolishi mumkin."
    ]

    @horoscopes = {
      "Qoy" => "Bugun energiyangiz yuqori! Yangi boshlovchilar uchun omadli kun!",
      "Buzoq" => "Sabrlilik muvaffaqiyat kalitidir. Moliyaviy ishlaringizni tekshiring!",
      "Egizaklar" => "Muloqot orqali kop narsaga erishasiz. Dostlaringiz bilan uchrashuv rejalashtiring.",
      "Qisqichbaqa" => "Oilaviy masalalar ustuvor. Yaqinlaringiz bilan vaqt otkazing!",
      "Arslon" => "Siz bugun kuchli va ishonchlisiz. Yangi imkoniyatlarni otkazib yubormang!",
      "Parizod" => "Mayda detallar ham muhim. Ishda tartib bilan shugullaning!",
      "Tarozi" => "Muvozanatni saqlang, sevgi hayotingizda muhim ozgarish bolishi mumkin.",
      "Chayon" => "Bugun ozingizni tahlil qiling. Katta qarorlarni ertaga qoldiring.",
      "Oqotar" => "Sayohat yoki sarguzasht uchun ideal kun!",
      "Tog echkisi" => "Mehnatingiz natijasini korasiz. Rahbarlar sizni sezishi mumkin!",
      "Qovga" => "Ijodiy goyalar uchun mukammal vaqt. Yangi loyihalarni boshlang!",
      "Baliq" => "Intuitsiyangiz kuchli. Bugun ichki sezgingizga ishonib qaror qabul qiling!"
    }
  end

  def get_horoscope(month)
    case month
    when 3..4 then "Qoy"
    when 4..5 then "Buzoq"
    when 5..6 then "Egizaklar"
    when 6..7 then "Qisqichbaqa"
    when 7..8 then "Arslon"
    when 8..9 then "Parizod"
    when 9..10 then "Tarozi"
    when 10..11 then "Chayon"
    when 11..12 then "Oqotar"
    when 12,1 then "Tog echkisi"
    when 1..2 then "Qovga"
    when 2..3 then "Baliq"
    else "Nomalum"
    end
  end

  def tell_fortune(name, age, month)
    puts "\n?? Bashorat tayyorlanmoqda... ??"
    sleep(2)

    horoscope = get_horoscope(month)
    
    puts "\n??? Salom, #{name}! Siz #{age} yoshdasiz va #{month}-oyda tugilgansiz."
    puts "???>? Sizning burjingiz: #{horoscope}"
    puts "\n?? **Burj bashorati:** #{@horoscopes[horoscope]} ??"
    
    sleep(2)
    
    # Tasodifiy bashorat chiqarish
    puts "\n?? **Bashorat:** #{@fortunes.sample} ??"
    
    # Omad darajasini hisoblash
    luck = rand(50..100)
    puts "\n?? **Bugungi omad darajangiz: #{luck}%**"
    
    # Tasodifiy maslahat chiqarish
    puts "\n?? **Maslahat:** #{@advice.sample}"

    # Qoshimcha hazil
    puts "\n?? **Hazil:** Agar bugun hayotingizdagi eng yaxshi kun bolmasa, hech bolmaganda eng yomoni emas!"

    save_to_json(name, age, month, horoscope, luck)
  end

  def save_to_json(name, age, month, horoscope, luck)
    data = {
      name: name,
      age: age,
      month: month,
      horoscope: horoscope,
      luck: luck
    }

    File.open("fortune_history.json", "a") do |file|
      file.puts(data.to_json)
    end
  end
end

# GUI interfeys
def create_gui
  root = TkRoot.new { title "AI Fortune Teller" }
  
  TkLabel.new(root) { text "Ismingizni kiriting:"; pack }
  name_entry = TkEntry.new(root)
  name_entry.pack

  TkLabel.new(root) { text "Yoshingizni kiriting:"; pack }
  age_entry = TkEntry.new(root)
  age_entry.pack

  TkLabel.new(root) { text "Tugilgan oy raqamingizni kiriting (1-12):"; pack }
  month_entry = TkEntry.new(root)
  month_entry.pack

  teller = FortuneTeller.new

  TkButton.new(root) do
    text "Bashorat olish"
    command proc {
      name = name_entry.get
      age = age_entry.get.to_i
      month = month_entry.get.to_i
      teller.tell_fortune(name, age, month)
    }
    pack
  end

  Tk.mainloop
end

# Boshlash
puts "Salom, men virtual bashoratchiman! ??"
print "Ismingizni kiriting: "
name = gets.chomp

print "Yoshingiz nechida? "
age = gets.chomp.to_i

print "Tugilgan oy raqamingizni kiriting (1-12): "
month = gets.chomp.to_i

fortune_teller = FortuneTeller.new
fortune_teller.tell_fortune(name, age, month)

# GUI-ni ishga tushirish
create_gui
