package com.example.logistics.manager;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Looper;
import android.util.Log;

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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class goodManager {

    private static final String TAG = "goodDao";

    public int search(String id, String phonenum, Context context){//搜索快件位置（id，手机号）

        Log.d(TAG, "search");
        HashMap<String, Object> map = new HashMap<>();

        Connection connection = DBOpenHelper.getConn();


        int msg = 0;
        try{
            Log.d(TAG, "select goods");
            String sql = "select * from goods where id = ?";

            if(connection != null){
                Log.d(TAG, "connection!");
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "id: " + id);
                    ps.setInt(1,Integer.parseInt(id));
                    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();
                    if(map.size() != 0){
                        Log.d(TAG, "map isnot null");
                        StringBuilder s = new StringBuilder();
                        for(String key:map.keySet()){
                            if(key.equals("phonenum")){
                                String strphone = String.valueOf(map.get(key));
                                Log.d(TAG, strphone);
                                if(phonenum.equals(strphone.substring(7,11))){
                                    msg = 1;        //phonenum correct
                                    Log.d(TAG, "phonenum correct");
                                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                    builder.setTitle("快件位置");
                                    builder.setMessage("\n第 "+ map.get("shelfnum")+ " 号货架\n第 " + map.get("floornum")+ " 层");
                                    Looper.prepare();
                                    builder.show();
                                    Looper.loop();
                                }else{
                                    Log.d(TAG, "phonenum incorrect");
                                    msg = 2;        //phonenum 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, "search bug: " + e.getMessage());
            msg = 0;
        }
        return msg;
    }

    public boolean delete(String pickupcode){

        Connection connection = DBOpenHelper.getConn();
        try{
            Log.d(TAG, "delete");
            String sql = "delete from goods where id = ?";

            if(connection != null){
                Log.d(TAG, "successCon");
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "pickupcode: " + pickupcode);
                    ps.setString(1,pickupcode);

                    ps.executeUpdate();

                    connection.close();
                    ps.close();
                    return true;
                }else{
                    Log.e(TAG, "ps is null");
                    return false;
                }
            }else{
                Log.e(TAG, "connection is null");
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
            Log.d(TAG, "delete bug" + e.getMessage());
        }
        return false;
    }

    public List<Map<String, Object>> nopick(String phonenum){

        Connection connection = DBOpenHelper.getConn();
        Log.d(TAG, "nopick goods");
        List<Map<String, Object>> datalist;
        datalist = new ArrayList<Map<String, Object>>();
        try{
            Log.d(TAG, "select");
            String sql = "select * from goods where phonenum = ?";

            if(connection != null){
                Log.d(TAG, "successCon");
                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()){

                        Log.d(TAG, "getResult");

                        Map<String, Object> map = new HashMap<String, Object>();
                        for(int i = 1; i <= count; i++){
                            String field = rs.getMetaData().getColumnName(i);
                            if(field.equals("id")){
                                Log.d(TAG, "id");
                                map.put(field, rs.getString(field));
                            }
                            if(field.equals("time")) {
                                Log.d(TAG, "time");
                                map.put(field, rs.getString(field));
                            }
                        }
                        datalist.add(map);
                    }
                    connection.close();
                    ps.close();
                }else{
                    Log.e(TAG, "ps is null");
                }
            }else{
                Log.e(TAG, "connection is null");
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.d(TAG, "search bug: " + e.getMessage());
        }
        return datalist;
    }

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

        Connection connection = DBOpenHelper.getConn();

        int msg = 0;
        try{
            Log.d(TAG, "pickup");
            String sql = "select * from goods where id=?";

            if(connection != null){
                Log.d(TAG, "successCon");
                PreparedStatement ps = connection.prepareStatement(sql);
                if(ps != null){
                    Log.e(TAG, "pickupcode: " + pickupcode);
                    ps.setInt(1,Integer.parseInt(pickupcode));
                    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();
                    if(map.size() != 0){
                        Log.d(TAG, "map has value");
                        StringBuilder s = new StringBuilder();
                        for(String key:map.keySet()){
                            if(key.equals("phonenum")){
                                if(phonenum.equals(map.get(key).toString().substring(7,11))){
                                    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;
    }

}
