require 'json'
require 'tk'

class TruthOrDare
  def initialize
    @truths = [
      "Eng katta siringiz nima?",
      "Songgi marta qachon yolgon gapirgansiz?",
      "Kimga eng katta yordam bergansiz?",
      "Agar super kuchga ega bolsangiz, nima bolishni xohlardingiz?",
      "Hech kimga aytmagan narsangiz bormi?"
    ]

    @dares = [
      "Telefon kontaktlaringizdan tasodifiy odamga kulgili SMS yozing!",
      "1 daqiqa davomida qolingiz bilan buruningizni ushlab turing.",
      "Qoshiq kuylang yoki sher oqib bering!",
      "Hozir siz turgan joyda sakrab 10 marta aylaning!",
      "Kimgadir xabar yozib: 'Men senga ishonaman!' deb ayting!"
    ]

    @reactions = [
      "Qiziq, bu haqiqatan ham rostmi? ??",
      "Juda qiziqarli javob! ??",
      "Siz haqiqatni aytdingiz, men ishonaman! ??",
      "Bu ajoyib javob! Sizga hurmatim baland! ??"
    ]

    @dare_reactions = [
      "Siz jasur odamsiz! ??",
      "Men buni kutmagandim! ??",
      "Barchamiz uchun misol boldingiz! ??",
      "Zor bajardingiz! ??"
    ]

    @stats = { truth: 0, dare: 0 }
  end

  def ask_truth
    question = @truths.sample
    puts "\n?? **Haqiqat savoli:** #{question}"
    print "?? Javobingiz: "
    answer = gets.chomp
    puts "\n?? **AI fikri:** #{@reactions.sample}"
    @stats[:truth] += 1
    save_to_json("Truth", question, answer)
  end

  def give_dare
    dare = @dares.sample
    puts "\n?? **Jasorat vazifasi:** #{dare}"
    print "? Bajardingizmi? (ha/yoq): "
    response = gets.chomp.downcase
    if response == "ha"
      puts "\n?? **AI fikri:** #{@dare_reactions.sample}"
      @stats[:dare] += 1
    else
      puts "\n? Siz vazifani bajarmadingiz! ??"
    end
    save_to_json("Dare", dare, response)
  end

  def save_to_json(type, question, answer)
    data = { type: type, question: question, answer: answer, time: Time.now }
    File.open("game_history.json", "a") { |file| file.puts(data.to_json) }
  end

  def show_stats
    puts "\n?? **Oyin statistikasi:**"
    puts "? Haqiqatlar: #{@stats[:truth]}"
    puts "?? Jasoratlar: #{@stats[:dare]}"
  end
end

# GUI interfeys
def create_gui
  root = TkRoot.new { title "AI Truth or Dare" }

  game = TruthOrDare.new

  TkLabel.new(root) { text "Haqiqat yoki Jasorat?"; pack }

  TkButton.new(root) do
    text "Haqiqat"
    command proc { game.ask_truth }
    pack
  end

  TkButton.new(root) do
    text "Jasorat"
    command proc { game.give_dare }
    pack
  end

  TkButton.new(root) do
    text "Statistika"
    command proc { game.show_stats }
    pack
  end

  Tk.mainloop
end

# ?? Oyin boshlash
game = TruthOrDare.new
puts "?? AI Truth or Dare oyiniga xush kelibsiz! ??"
loop do
  puts "\n1?? Haqiqat\n2?? Jasorat\n3?? Statistikani korish\n4?? Chiqish"
  print "?? Tanlovingiz: "
  choice = gets.chomp.to_i

  case choice
  when 1
    game.ask_truth
  when 2
    game.give_dare
  when 3
    game.show_stats
  when 4
    puts "?? Oyinni tark etdingiz. Xayr!"
    break
  else
    puts "? Notogri tanlov! Qaytadan urinib koring!"
  end
end

# GUI ni ishga tushirish
create_gui
