package com.qy.agv.activity.dao;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.qy.agv.activity.model.RelocModel;
import com.qy.agv.activity.model.RobotModel;
import com.qy.agv.activity.model.ShelfModel;
import com.qy.agv.activity.model.SiteModel;
import com.qy.agv.util.Constant;
import com.qy.agv.util.HttpsUtil;
import com.qy.agv.util.ToastUtils;

import org.apache.log4j.Logger;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

public class MainDao {
    public static Logger mylog =Logger.getLogger(MainDao.class);
    private static MainDao dao = null;

    public static MainDao getInstance(){
        if(dao == null){
            dao = new MainDao();
        }
        return dao;
    }

    public List<RobotModel> searchRobot(Context context) {
        List<RobotModel> robotModels = new ArrayList<>();
        RequestParams params = HttpsUtil.httParams(Constant.ROBOT_LIST);
        params.addBodyParameter("current", "1");
        params.addBodyParameter("size", "100");
        try {
            AtomicReference<JSONObject> rsAtm = new AtomicReference<>(new JSONObject());
            Thread s1 = new Thread(()->{
                try {
                    rsAtm.set(x.http().getSync(params, JSONObject.class));
                }catch (Throwable e) {
                    e.printStackTrace();
                }
            });
            try{
                s1.start();
                s1.join();
            }catch (Exception e){
                e.printStackTrace();
            }
            JSONObject rs = rsAtm.get();
            if (rs.has("code") && rs.getInt("code") != 200) {
                ToastUtils.showToast(context, "请求失败");
            }
            if (rs.has("success")) {
                System.out.println(rs.get("data"));
                Type rds = new TypeToken<Map<String, JsonElement>>() {
                }.getType();
                Map<String, JsonElement> records = new Gson().fromJson(rs.get("data").toString(), rds);
                System.out.println(records.get("records"));
                Type lt = new TypeToken<ArrayList<RobotModel>>() {}.getType();
                Object rs_record = records.get("records");
                if(rs_record!=null) {
                    robotModels = new Gson().<List<RobotModel>>fromJson(rs_record.toString(), lt);
                }
            }
        }catch (Throwable e) {
            e.printStackTrace();
            mylog.error("加载机器人列表异常：", e);
        }
        return robotModels;
    }

    public List<ShelfModel> searchShelf(Context context) {
        List<ShelfModel> shelfModels = new ArrayList<>();
        RequestParams params = HttpsUtil.httParams(Constant.SHELF_LIST);
        params.addBodyParameter("current", "1");
        params.addBodyParameter("size", "100");
        try {
            AtomicReference<JSONObject> rsAtm = new AtomicReference<>(new JSONObject());
            Thread s1 = new Thread(()->{
                try {
                    rsAtm.set(x.http().getSync(params, JSONObject.class));
                }catch (Throwable e) {
                    e.printStackTrace();
                }
            });
            try{
                s1.start();
                s1.join();
            }catch (Exception e){
                e.printStackTrace();
            }
            JSONObject rs = rsAtm.get();
            if (rs.has("code") && rs.getInt("code") != 200) {
                ToastUtils.showToast(context, "请求失败");
            }
            if (rs.has("success")) {
                System.out.println(rs.get("data"));
                Type rds = new TypeToken<Map<String, JsonElement>>() {
                }.getType();
                Map<String, JsonElement> records = new Gson().fromJson(rs.get("data").toString(), rds);
                System.out.println(records.get("records"));
                Type lt = new TypeToken<ArrayList<ShelfModel>>() {}.getType();
                Object rs_record = records.get("records");
                if(rs_record!=null) {
                    shelfModels = new Gson().fromJson(rs_record.toString(), lt);
                }
            }
        }catch (Throwable e) {
            e.printStackTrace();
            mylog.error("加载货架列表异常：", e);
        }
        return shelfModels;
    }

    public List<SiteModel> searchSite(Context context) {
        List<SiteModel> siteModels = new ArrayList<>();
        RequestParams params = HttpsUtil.httParams(Constant.SITE_LIST);
        params.addBodyParameter("current", "1");
        params.addBodyParameter("size", "100");
        try {
            AtomicReference<JSONObject> rsAtm = new AtomicReference<>(new JSONObject());
            Thread s1 = new Thread(()->{
                try {
                    rsAtm.set(x.http().getSync(params, JSONObject.class));
                }catch (Throwable e) {
                    e.printStackTrace();
                }
            });
            try{
                s1.start();
                s1.join();
            }catch (Exception e){
                e.printStackTrace();
            }
            JSONObject rs = rsAtm.get();
            if (rs.has("code") && rs.getInt("code") != 200) {
                ToastUtils.showToast(context, "请求失败");
            }
            if (rs.has("success")) {
                System.out.println(rs.get("data"));
                Type rds = new TypeToken<Map<String, JsonElement>>() {
                }.getType();
                Map<String, JsonElement> records = new Gson().fromJson(rs.get("data").toString(), rds);
                System.out.println(records.get("records"));
                Type lt = new TypeToken<ArrayList<SiteModel>>() {}.getType();
                Object rs_record = records.get("records");
                if(rs_record!=null) {
                    siteModels = new Gson().fromJson(rs_record.toString(), lt);
                }
            }
        }catch (Throwable e) {
            e.printStackTrace();
            mylog.error("加载站点列表异常：", e);
        }
        return siteModels;
    }

    public List<RelocModel> searchReloc(Context context) {
        List<RelocModel> relocModels = new ArrayList<>();
        RequestParams params = HttpsUtil.httParams(Constant.RELOC_LIST);
        params.addBodyParameter("current", "1");
        params.addBodyParameter("size", "100");
        try {
            AtomicReference<JSONObject> rsAtm = new AtomicReference<>(new JSONObject());
            Thread s1 = new Thread(()->{
                try {
                    rsAtm.set(x.http().getSync(params, JSONObject.class));
                }catch (Throwable e) {
                    e.printStackTrace();
                }
            });
            try{
                s1.start();
                s1.join();
            }catch (Exception e){
                e.printStackTrace();
            }
            JSONObject rs = rsAtm.get();
            if (rs.has("code") && rs.getInt("code") != 200) {
                ToastUtils.showToast(context, "请求失败");
            }
            if (rs.has("success")) {
                System.out.println(rs.get("data"));
                Type rds = new TypeToken<Map<String, JsonElement>>() {
                }.getType();
                Map<String, JsonElement> records = new Gson().fromJson(rs.get("data").toString(), rds);
                System.out.println(records.get("records"));
                Type lt = new TypeToken<ArrayList<RelocModel>>() {}.getType();
                Object rs_record = records.get("records");
                if(rs_record!=null) {
                    relocModels = new Gson().<List<RelocModel>>fromJson(rs_record.toString(), lt);
                }
            }
        }catch (Throwable e) {
            e.printStackTrace();
            mylog.error("加载重定位点列表异常：", e);
        }
        return relocModels;
    }

    public SiteModel querySiteName(Context context, String site) {
        SiteModel siteModel = new SiteModel();
        RequestParams params = HttpsUtil.httParams(Constant.SITE_DETAIL);
        params.addBodyParameter("locationId", site);
        try {
            AtomicReference<JSONObject> rsAtm = new AtomicReference<>(new JSONObject());
            Thread s1 = new Thread(()->{
                try {
                    rsAtm.set(x.http().getSync(params, JSONObject.class));
                }catch (Throwable e) {
                    e.printStackTrace();
                }
            });
            try{
                s1.start();
                s1.join();
            }catch (Exception e){
                e.printStackTrace();
            }
            JSONObject rs = rsAtm.get();
            if (rs.has("code") && rs.getInt("code") != 200) {
                ToastUtils.showToast(context, "请求失败");
            }
            if (rs.has("success")) {
                System.out.println(rs.get("data"));
                Object data_o = rs.get("data");
                siteModel = new Gson().fromJson(data_o.toString(), SiteModel.class);
            }
        }catch (Throwable e) {
            e.printStackTrace();
            mylog.error("加载站点异常：", e);
        }
        return siteModel;
    }
}
