package core.user.utils;

import core.user.Student;
import core.user.User;
import core.utils.GetObjectByName;
import dao.DBManagement;
import error.GExcptAccount;
import error.GExcptSQL;
import utils.Utils;

import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static dao.DBManagement.userTables;

public interface AccountManagement {
    static User login(String id, String password) throws Exception {
        List<String[]> ls = null;
        String userType = null;
        for(String userTable:userTables){
            List<String> columns = new ArrayList<>();
            columns.add("*");
            Map<String,String> limits = new HashMap<>();
            limits.put("id",id);
            try {
                ls = DBManagement.select(columns,userTable,limits,0,1);
            } catch (Exception e) {
                throw new GExcptSQL("QUERY\n\t"+id+"\nfailure");
            }
            if(ls.size()!=0){
                userType = userTable;
                break;
            }
        }
        String[] userInfo = ls.get(0);
        if(!userInfo[1].equals(password))
            throw new GExcptAccount("password wrong");
        Map<String, String> vMap = new HashMap<>();
        String[] info = DBManagement.getUserInfoTables(userType);
        for(int i=0;i<userInfo.length;i++){
            vMap.put(info[i],userInfo[i]);
        }
        return createUser(userType,vMap);
    }
    static User register(String userType, Map<String, String> vMap) throws Exception {
        DBManagement.insert(userType,vMap);
        if(userType.equals("student")){
            initAllStatus(vMap.get("id"));
        }
        User user =  createUser(userType, vMap);
        return  user;
    }
    static void logout(User user){
        //todo
    }
    static void destroy(User user){
        //todo
    }
    static User createUser(String userType, Map<String, String> vMap) throws Exception {
        User user = GetObjectByName.getUserByName(userType);
        user.setAttr(vMap);
        user.initCondition();
        return user;
    }
    String defaultStatus = "1";
    static void initAllStatus(String id) throws GExcptSQL {
        for(String table:DBManagement.graduationDesignTables){
            Map<String,String> vMap = new HashMap<>();
            vMap.put("id",id);
            DBManagement.insert(table,vMap);
        }
        for(String[] s:DBManagement.allStatus){
            Map<String,String> vMap = new HashMap<>();
            Map<String,String> limits = new HashMap<>();
            limits.put("id",id);
            vMap.put(s[1],defaultStatus);
            DBManagement.update(s[0],vMap,limits);
        }
        //
        Map<String,String> vMap = new HashMap<>();
        Map<String,String> limits = new HashMap<>();
        limits.put("id",id);
        vMap.put("student_id",id);
        DBManagement.update("graduation_design",vMap,limits);
    }
}
