package view;

import dao.indentItemAbstractDAO;
import dao.specification.indentDAO;
import dao.specification.indentItemDAO;
import entities.IndentItem;
import util.Toolset;

import java.util.Date;
import java.util.Objects;
import java.util.Scanner;

public class IndentItemView {
    public void IndentItemDB(int chose) throws IllegalAccessException {

        switch (chose) {
            case 0 -> System.exit(0);
            case 1 -> DeleteIItem();
            case 2 -> UpdateIItem();
            case 3 -> SearchIItem(true);//查询全部
            case 4 -> SearchIItem(false);//查询ID
        }
    }

    public void CreateIItem(Long indentID) {
        indentItemAbstractDAO DML_insert = new indentItemDAO();
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入：菜名|初始价格|折扣|描述 退出请输入end");

        int flag = 0;
        while (!reader.hasNext("end")) {
            IndentItem IItem = new IndentItem();
            String s;
            String[] sn;
            while (!reader.hasNext("end")) {
                s = reader.nextLine();
                sn = s.split("\\s+");
                //菜名
                IItem.setName(sn[0]);
                //初始价格
                IItem.setInitialPrice(Float.valueOf(sn[1]));
                //折扣
                if (sn[3] != null) {
                    IItem.setDiscount(Float.valueOf(sn[2]));
                } else {
                    IItem.setDiscount(-1F);
                }
                //描述
                if (sn[3] != null) {
                    IItem.setDescription(sn[3]);
                }
            }
//            IItem.setName(reader.next());
//            IItem.setInitialPrice(reader.nextFloat());

//            if (reader.hasNextFloat()) {
//                IItem.setDiscount(reader.nextFloat());
//            } else {
//                IItem.setDiscount(-1F);
//            }

//            if (!reader.hasNext("end") && !Objects.equals(reader.nextLine(), " ")) {
//                IItem.setDescription(reader.nextLine());
//            }
            //菜单ID
            indentDAO indent = new indentDAO();
            IItem.setIndentID(indent.searchID(indentID));

            flag += DML_insert.insert(IItem);
        }
        System.out.println(flag + "行受影响");
    }

    private void DeleteIItem() {
        indentItemAbstractDAO DML_delete = new indentItemDAO();
        Scanner reader = new Scanner(System.in);
        IndentItem IItem = new IndentItem();

        System.out.println("请输入你要删除的ID");
        IItem.setId(reader.nextLong());

        int flag = DML_delete.delete(IItem);
        System.out.println(flag + "行受影响");
    }

    //TODO 更新订单菜品
    private void UpdateIItem() {
        indentItemAbstractDAO DML_update = new indentItemDAO();
        Scanner reader = new Scanner(System.in);
        IndentItem IItem = new IndentItem();

    }

    private void SearchIItem(boolean b) throws IllegalAccessException {
        indentItemAbstractDAO DQL = new indentItemDAO();
        Scanner reader = new Scanner(System.in);
        IndentItem IItem = new IndentItem();

        if (b) {
            for (IndentItem item : DQL.search(IItem)) {
                System.out.println(Toolset.table(IndentItem.class, item));
            }
        } else {
            System.out.println("请输入你要查询的ID:");
            IItem = DQL.searchID(reader.nextLong());
            System.out.println(Toolset.table(IndentItem.class, IItem));
        }
    }
}
