# import yaml


class Book():
    def __init__(self, bid, name, price, summary):
        self.bid = bid
        self.name = name
        self.price = price
        self.summary = summary

    def __str__(self):
        return f"bid:{self.bid},name:{self.name},price:{self.price},summary:{self.summary}"

    def to_dict(self):
        return {
            'bid': self.bid,
            'name': self.name,
            'price': self.price,
            'summary': self.summary
        }


class BookManagement():

    def __init__(self):
        self.__books = []
        # self.load_data()

    def __menu(self):
        '''
        展示菜单
        :return: 选择的编号
        '''
        print("*****************************")
        print("*      图书管理系统           *")
        print("* 1. 添加新图书信息           *")
        print("* 2. 通过编号修改图书信息      *")
        print("* 3. 通过编号删除图书信息      *")
        print("* 4. 通过书名删除图书信息      *")
        print("* 5. 通过编号查询图书信息      *")
        print("* 6. 通过书名查询图书信息      *")
        print("* 7. 显示所有图书信息         *")
        print("* 8. 退出系统                *")
        print("*****************************")
        # 用户输入
        select_op = input("请输入菜单中编号选择:")
        return select_op

    def __get_bid(self):
        '''
        获取编号
        :return:编号
        '''
        bid = input("请输入书的编号：")
        return bid

    def __get_name(self):
        '''
        获取书名
        :return:书名
        '''
        name = input("请输入书的名称：")
        return name

    def __get_price(self):
        '''
        获取书价
        :return: 书的价格
        '''
        while True:
            price = input("请输入书的价格：")
            if price.isdigit():
                return price
            else:
                print(f"输入的价格{price}不合法，请重新输入")

    def __get_summary(self):
        '''
        获取书的简介
        :return: 书的简介
        '''
        summary = input("请输入书的简介")
        return summary

    def add_book(self, bid, name, price, summary):
        '''
        添加图书信息
        :param bid: 编号
        :param name: 书名
        :param price: 价格
        :param summary: 简介
        :return: 添加结果
        '''
        for s in self.__books:
            if s.bid == bid:
                print("该编号存在，请重新添加")
                return "返回失败结果"
        else:
            book = Book(bid, name, price, summary)
            self.__books.append(book)
            print(f"添加{book}信息成功")
            return "返回成功结果"

    def modify_book_by_id(self, bid):
        '''
        通过编号修改图书信息
        :param bid: 编号
        :return: 修改结果
        '''
        for s in self.__books:
            if s.bid == bid:
                name = self.__get_name()
                price = self.__get_price()
                summary = self.__get_summary()
                s.name = name
                s.price = price
                s.summary = summary
                print(f"修改编号为{bid}的图书信息完成")
                return "修改成功"
        else:
            print(f"目前没有编号为{bid}的图书，请重新输入")
            return "修改失败"

    def delete_book_by_id(self, bid):
        '''
        通过编号删除图书信息
        :param bid: 编号
        :return: 删除结果
        '''
        for s in self.__books:
            if s.bid == bid:
                self.__books.remove(s)
                print(f"已删除编号为{bid}的图书信息")
                return "返回成功结果"
        print(f"未找到编号为{bid}的图书，请重新输入")
        return "返回失败结果"

    def delete_book_by_name(self, name):
        '''
        通过书名删除图书信息
        :param name: 书名
        :return: 删除结果
        '''
        exist_books = []
        for s in self.__books:
            if s.name == name:
                exist_books.append(s)
        if len(exist_books) > 0:
            for a in exist_books:
                self.__books.remove(a)
                print(f"编号为{a.bid}的图书删除成功")
            print(f"成功删除了{len(exist_books)}本图书信息")
            return "返回成功结果"
        else:
            print(f"没有找到名称为{name}的图书，请重新输入")
            return "返回失败结果"

    def query_book_by_id(self, bid):
        '''
        通过编号查询图书信息
        :param bid: 编号
        :return: 查询结果
        '''
        for s in self.__books:
            if s.bid == bid:
                print("查询结果如下：")
                print(f"编号：{s.bid},书名：{s.name}，价格：{s.price}，介绍：{s.summary}")
                return "返回成功结果"
        print(f"编号{bid}的图书不存在，请重新输入")
        return "返回失败结果"

    def query_book_by_name(self, name):
        '''
        通过书名查询图书信息
        :param name: 书名
        :return: 查询结果
        '''
        exist_books = []
        for s in self.__books:
            if s.name == name:
                exist_books.append(s)
        if len(exist_books) > 0:
            for a in exist_books:
                print(f"编号：{a.bid},书名：{a.name}，价格：{a.price}，介绍：{a.summary}")
            print(f"共查询到{len(exist_books)}本图书信息")
            return "返回成功结果"
        else:
            print(f"未查到书名为{name}的图书，请重新输入")
            return "返回失败结果"

    def show(self):
        '''
        查询所有图书
        :return:查询结果
        '''
        for s in self.__books:
            print(f"编号：{s.bid},书名：{s.name}，价格：{s.price}，介绍：{s.summary}")

    # def save_data(self):
    #     '''
    #     数据保存到 book_data.yaml 文件中
    #     :return:
    #     '''
    #     if len(self.__books) > 0:
    #         # 将 Book 对象列表转为字典列表
    #         books_data = [book.to_dict() for book in self.__books]
    #         with open("book_data.yaml", "w", encoding="utf-8") as data:
    #             yaml.safe_dump(books_data, data, allow_unicode=True, default_flow_style=False, indent=2)
    #         print("退出成功，数据保存到yaml中")
    #         return "保存yaml文件"
    #     else:
    #         print("没有数据，成功退出")
    #         return "退出成功"
    #
    # def load_data(self):
    #     """
    #     从 YAML 文件加载图书数据
    #     """
    #     try:
    #         with open("book_data.yaml", "r", encoding="utf-8") as file:
    #             data = yaml.safe_load(file)
    #             if data is None:
    #                 self.__books = []
    #             else:
    #                 self.__books = [Book(item['bid'], item['name'], item['price'], item['summary']) for item in data]
    #         print("图书数据加载成功！")
    #     except FileNotFoundError:
    #         self.__books = []
    #         print("未找到数据文件，初始化空图书列表。")
    #     except Exception as e:
    #         print(f"加载数据失败：{e}")
    #         self.__books = []

    def book_manager(self):
        '''
        图书管理系统7
        :return:
        '''
        while True:
            # 展示菜单
            select_op = self.__menu()
            # 判断用户选择的1-8之间的数字
            if len(select_op) == 1 and select_op in "12345678":
                # 输入1，添加图书
                if select_op == "1":
                    # 接收用户输入的信息
                    bid = self.__get_bid()
                    name = self.__get_name()
                    price = self.__get_price()
                    summary = self.__get_summary()
                    # 调用添加图书方法
                    self.add_book(bid, name, price, summary)
                # 输入2，修改图书信息
                elif select_op == "2":
                    bid = self.__get_bid()
                    self.modify_book_by_id(bid)
                # 输入3，通过编号删除图书
                elif select_op == "3":
                    bid = self.__get_bid()
                    self.delete_book_by_id(bid)
                # 输入4，通过书名删除图书
                elif select_op == "4":
                    name = self.__get_name()
                    self.delete_book_by_name(name)
                # 输入5，通过编号查询图书
                elif select_op == "5":
                    bid = self.__get_bid()
                    self.query_book_by_id(bid)
                # 输入6，通过书名查询图书
                elif select_op == "6":
                    name = self.__get_name()
                    self.query_book_by_name(name)
                # 输入7，显示所有图书信息
                elif select_op == "7":
                    self.show()
                # 输入8
                else:
                    # self.save_data()
                    break
            else:
                print("输入字段不合法，请重新输入")


if __name__ == '__main__':
    s = BookManagement()
    s.book_manager()