package view;

import dao.specification.userDAO;
import dao.userAbstractDAO;
import entities.Users;
import util.Toolset;

import java.util.Scanner;

//TODO 用户密码查找和修改
public class UserView {
    //用户数据库操作
    protected void userDB(int chose) throws IllegalAccessException {


        switch (chose) {
            case 0 -> System.exit(0);
            case 1 -> CreateUser();
            case 2 -> DeleteUser();
            case 3 -> UpdateUser();
            case 4 -> SearchUser(true);//查询全部
            case 5 -> SearchUser(false);//查询ID
        }
    }

    private void CreateUser() {//TODO 非换行输入
        Scanner reader = new Scanner(System.in);
        userAbstractDAO DML_insert = new userDAO();

        System.out.println("请输入：名字|电话|地址|密码");
        Users user = new Users
                (reader.nextLine(), reader.nextLine(), reader.nextLine(), reader.nextLine());

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

    private void DeleteUser() {
        Users user = new Users();
        Scanner reader = new Scanner(System.in);

        userAbstractDAO DML_delete = new userDAO();

        System.out.println("请输入你要删除的ID:");
        user.setId(reader.nextLong());
        int flag = DML_delete.delete(user);

        System.out.println(flag + "行受影响");
    }

    private void UpdateUser() {//@FIXME update没有传值
        Users user = new Users();
        ;
        Scanner reader = new Scanner(System.in);

        userAbstractDAO DML_update = new userDAO();
        int flag = DML_update.update(user);
        System.out.println(flag + "行受影响");
    }

    private void SearchUser(boolean b) throws IllegalAccessException {
        Users user = new Users();
        Scanner reader = new Scanner(System.in);
        userAbstractDAO DQL = new userDAO();
        if (b) {
            //格式化输出
            for (Users item : DQL.search(user)) {
                System.out.println(Toolset.table(Users.class, item));
            }
        } else {
            System.out.println("请输入你要查询的ID:");
            user = DQL.searchID(reader.nextLong());
            //格式化输出
            System.out.println(Toolset.table(Users.class, user));
        }
    }
}
