package com.example.logistics.manager;

import android.util.Log;

import com.example.logistics.entity.User;
import com.example.logistics.tools.DBOpenHelper;
import com.example.logistics.tools.MD5Utils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.sql.ResultSet;
import java.sql.Connection;

public class userManager {

    private static final String TAG = "userDao";

    public int login(String username, String password){
        HashMap<String, Object> map = new HashMap<>();

        Connection connection = DBOpenHelper.getConn();

        int msg = 0;
        try{
            String sql = "select * from users where username = ?";

            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "username: " + username);
                    ps.setString(1,username);
                    ResultSet rs = ps.executeQuery();

                    int count = rs.getMetaData().getColumnCount();
                    while(rs.next()){
                        for(int i = 1; i <= count; i++){
                            String field = rs.getMetaData().getColumnName(i);
                            map.put(field, rs.getString(field));
                        }
                    }
                    connection.close();
                    ps.close();
                    password = MD5Utils.md5(password);
                    if(map.size() != 0){
                        StringBuilder s = new StringBuilder();
                        for(String key:map.keySet()){
                            if(key.equals("password")){

                                if(password.equals(map.get(key))){
                                    msg = 1;        //password correct
                                }else{
                                    msg = 2;        //password incorrect
                                }
                                break;
                            }
                        }
                    }else{
                        Log.e(TAG, "result is null");
                        msg = 3;                    //result is null
                    }
                }else{
                    Log.e(TAG, "ps is null");
                    msg = 0;
                }
            }else{
                Log.e(TAG, "connection is null");
                msg = 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "login bug: " + e.getMessage());
            msg = 0;
        }
        return msg;
    }

    public int loginByPhone(String phonenum, String password){
        HashMap<String, Object> map = new HashMap<>();

        Connection connection = DBOpenHelper.getConn();

        int msg = 0;
        try{
            String sql = "select * from users where phonenum = ?";

            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "phonenum: " + phonenum);
                    ps.setString(1,phonenum);
                    ResultSet rs = ps.executeQuery();

                    int count = rs.getMetaData().getColumnCount();
                    while(rs.next()){
                        for(int i = 1; i <= count; i++){
                            String field = rs.getMetaData().getColumnName(i);
                            map.put(field, rs.getString(field));
                        }
                    }
                    connection.close();
                    ps.close();
                    password = MD5Utils.md5(password);
                    if(map.size() != 0){
                        StringBuilder s = new StringBuilder();
                        for(String key:map.keySet()){
                            if(key.equals("password")){

                                if(password.equals(map.get(key))){
                                    msg = 1;        //password correct
                                }else{
                                    msg = 2;        //password incorrect
                                }
                                break;
                            }
                        }
                    }else{
                        Log.e(TAG, "result is null");
                        msg = 3;                    //result is null
                    }
                }else{
                    Log.e(TAG, "ps is null");
                    msg = 0;
                }
            }else{
                Log.e(TAG, "connection is null");
                msg = 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "login bug: " + e.getMessage());
            msg = 0;
        }
        return msg;
    }

    public User findUser(String username){

        Connection connection = DBOpenHelper.getConn();
        User user = null;
        try{
            String sql = "select * from users where username = ?";
            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    ps.setString(1, username);
                    ResultSet rs = ps.executeQuery();

                    while(rs.next()){
                        int userId = rs.getInt(1);
                        String userName = rs.getString(2);
                        String passWord = rs.getString(3);
                        String phoneNum = rs.getString(4);

                        user = new User(userId, userName, passWord, phoneNum);
                    }
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    public boolean register(User user){
        HashMap<String, Object> map = new HashMap<>();

        Connection connection = DBOpenHelper.getConn();
        try{
            String sql = "insert into users(username, password, phonenum) values (?,?,?)";

            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);

                if(ps != null){
                    ps.setString(1, user.getUserName());
                    ps.setString(2, MD5Utils.md5(user.getPassWord()));
                    ps.setString(3, user.getPhoneNum());

                    int rs = ps.executeUpdate();
                    if(rs > 0){
                        return true;
                    }else{
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
            Log.e(TAG, "异常 register");
            return false;
        }
    }

    public int change(String phonenum, String password){
        HashMap<String, Object> map = new HashMap<>();

        Connection connection = DBOpenHelper.getConn();

        int msg = 0;
        try{
            String sql = "update users set password = ? where phonenum = ?";

            if(connection != null){
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "phonenum: " + phonenum);
                    password = MD5Utils.md5(password);
                    ps.setString(1,password);

                    ps.setString(2,phonenum);
                    ps.executeUpdate();
                    connection.close();
                    ps.close();
                    msg = 1;

                }else{
                    Log.e(TAG, "ps is null");
                    msg = 0;
                }
            }else{
                Log.e(TAG, "connection is null");
                msg = 0;
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "login bug: " + e.getMessage());
            msg = 0;
        }
        return msg;
    }

}
