import tkinter as tk
import random
from datetime import datetime, timedelta

# กำหนดราคาห้องตามสี
ROOM_PRICES = {
    "Green": 50,
    "Yellow": 100,
    "Blue": 200,
    "Red": 500
}

# กำหนดห้องทั้งหมดในโรงแรม (9 ชั้น x 4 ห้อง)
FLOORS = 9
ROOMS_PER_FLOOR = 4

class HotelManagement:
    def __init__(self, root):
        self.root = root
        self.root.title("Hotel Management Game")

        self.money = 1000  # เงินเริ่มต้นของผู้เล่น
        self.current_day = 1  # ระบบเวลาในเกม (เริ่มจากวันที่ 1)
        self.rooms = {}  # เก็บข้อมูลห้องพัก

        # สร้างห้องพัก
        colors = ["Green", "Yellow", "Blue", "Red"]
        for floor in range(1, FLOORS + 1):
            for room in range(1, ROOMS_PER_FLOOR + 1):
                room_number = f"{floor}-{room}"
                self.rooms[room_number] = {
                    "color": random.choice(colors),
                    "occupied": False,
                    "customer": None,
                    "checkout_day": None
                }

        # UI
        self.money_label = tk.Label(root, text=f"Money: ${self.money}", font=("Arial", 14))
        self.money_label.pack()

        self.day_label = tk.Label(root, text=f"Day: {self.current_day}", font=("Arial", 14))
        self.day_label.pack()

        self.room_listbox = tk.Listbox(root, width=50, height=15)
        self.room_listbox.pack()
        self.update_room_list()

        self.new_customer_button = tk.Button(root, text="New Customer", command=self.new_customer)
        self.new_customer_button.pack()

        self.checkin_button = tk.Button(root, text="Check-in", command=self.check_in)
        self.checkin_button.pack()

        self.checkout_button = tk.Button(root, text="Check-out", command=self.check_out)
        self.checkout_button.pack()

        self.next_day_button = tk.Button(root, text="Next Day", command=self.next_day)
        self.next_day_button.pack()

        self.customer = None  # ลูกค้าที่มาขอจองห้อง

    def update_room_list(self):
        """อัปเดตรายการห้องใน Listbox"""
        self.room_listbox.delete(0, tk.END)
        for room, info in self.rooms.items():
            status = "Available" if not info["occupied"] else f"Occupied by {info['customer']['name']}"
            self.room_listbox.insert(tk.END, f"Room {room} - {info['color']} - {status}")

    def new_customer(self):
        """สร้างลูกค้าใหม่ที่ต้องการเข้าพัก"""
        names = ["Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace", "Hannah"]
        name = random.choice(names)
        budget = random.randint(50, 600)  # งบประมาณลูกค้า
        nights = random.randint(1, 5)  # จำนวนคืนที่ต้องการพัก
        self.customer = {"name": name, "budget": budget, "nights": nights}
        tk.messagebox.showinfo("New Customer", f"{name} wants to stay for {nights} nights with a budget of ${budget}")

    def check_in(self):
        """เช็กอินลูกค้าเข้าห้อง"""
        if not self.customer:
            tk.messagebox.showwarning("Warning", "No customer waiting!")
            return

        selected_index = self.room_listbox.curselection()
        if not selected_index:
            tk.messagebox.showwarning("Warning", "Select a room!")
            return

        room_number = self.room_listbox.get(selected_index[0]).split()[1]
        room_info = self.rooms[room_number]

        if room_info["occupied"]:
            tk.messagebox.showwarning("Warning", "Room is already occupied!")
            return

        room_price = ROOM_PRICES[room_info["color"]]

        if self.customer["budget"] < room_price:
            tk.messagebox.showwarning("Warning", "Customer cannot afford this room!")
            return

        # ดำเนินการเช็กอิน
        total_price = room_price * self.customer["nights"]
        self.money += total_price
        self.rooms[room_number]["occupied"] = True
        self.rooms[room_number]["customer"] = self.customer
        self.rooms[room_number]["checkout_day"] = self.current_day + self.customer["nights"]
        self.customer = None  # ล้างลูกค้าปัจจุบัน

        self.money_label.config(text=f"Money: ${self.money}")
        self.update_room_list()

    def check_out(self):
        """เช็กเอาต์ลูกค้าจากห้อง"""
        selected_index = self.room_listbox.curselection()
        if not selected_index:
            tk.messagebox.showwarning("Warning", "Select a room!")
            return

        room_number = self.room_listbox.get(selected_index[0]).split()[1]
        room_info = self.rooms[room_number]

        if not room_info["occupied"]:
            tk.messagebox.showwarning("Warning", "Room is not occupied!")
            return

        self.rooms[room_number]["occupied"] = False
        self.rooms[room_number]["customer"] = None
        self.rooms[room_number]["checkout_day"] = None
        self.update_room_list()

    def next_day(self):
        """ข้ามไปวันถัดไป"""
        self.current_day += 1
        self.day_label.config(text=f"Day: {self.current_day}")

        # เช็กเอาต์ลูกค้าที่ถึงวันออก
        for room, info in self.rooms.items():
            if info["occupied"] and info["checkout_day"] == self.current_day:
                self.rooms[room]["occupied"] = False
                self.rooms[room]["customer"] = None
                self.rooms[room]["checkout_day"] = None

        self.update_room_list()


if __name__ == "__main__":
    root = tk.Tk()
    game = HotelManagement(root)
    root.mainloop()