package com.persagy.bdtp.adm.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.persagy.bdtp.adm.client.OldAdmClient;
import com.persagy.bdtp.adm.entity.db.AdmM2dEquip;
import com.persagy.bdtp.adm.entity.Pagination;
import com.persagy.bdtp.adm.entity.oldadm.*;
import com.persagy.bdtp.adm.service.IConfigService;
import com.persagy.bdtp.adm.service.IQueryFromOldAdm;
import com.persagy.bdtp.adm.service.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;
import java.util.function.Supplier;

@Service
public class QueryFromOldAdmImpl implements IQueryFromOldAdm {

	private static String user = "bdtp";

	private static String password = "123456";

	private static String appId = "adm";

	@Autowired
	private OldAdmClient admClient;

	@Autowired
	private IConfigService configService;

	@Autowired
	private ObjectMapper objectMapper;

	@Override
	public String login() {
		HashMap<String, Object> params = new HashMap<>();
		params.put("name", user);
		params.put("password", password);
		Map<String, Object> longinInfo = admClient.login(params);
		if("Success".equals(longinInfo.get("code"))){
			Map data = (Map)longinInfo.get("data");
			return (String) data.get("token");
		}
		return null;
	}

	@Override
	public List<OProject> queryProject(String token, String groupCode, String projectId) {
		return ServiceUtil.callOldAdm(() -> admClient.queryProject(groupCode, projectId, appId, user, token, new HashMap<>()));
	}

	@Override
	public List<OBuilding> queryBdAnFl(String token, String groupCode, String projectId) {
		HashMap<String,Object> params = new HashMap<>();
		HashMap<String, Object> cascade = new HashMap<>();
		cascade.put("name", "floor");
		cascade.put("orders", "floorSequenceID asc");
		params.put("cascade", Arrays.asList(cascade));
		return ServiceUtil.callOldAdm(() -> admClient.queryBuilding(groupCode, projectId, appId, user, token, params));
	}

	@Override
	public List<OSpace> querySpace(String token, String groupCode, String projectId) {
		HashMap<String,Object> params = new HashMap<>();
		params.put("zoneTypeList", Arrays.asList("GeneralZone", "OtherZone"));
		//params.put("zoneType", "GeneralZone");

		Pagination pagination = new Pagination(1000);
		return queryAllPage(() -> admClient.querySpace(groupCode, projectId, appId, user, token, params), params, pagination);
	}

	private <T> List<T> queryAllPage(Supplier<OldAdmResult<List<T>>> action, Map<String,Object> params, Pagination pagination){
		List<T> list = null;
		while (pagination.isHasNext()){
			params.put("pageNumber", pagination.getPageNum());
			params.put("pageSize", pagination.getPageSize());
			if(list == null)
				list = ServiceUtil.callOldAdm(action, pagination);
			else
				list.addAll(ServiceUtil.callOldAdm(action, pagination));
			pagination.next();
		}
		return list;
	}

	@Override
	public List<OEquip> queryEquip(String token, String groupCode, String projectId) {
		HashMap<String,Object> params = new HashMap<>();
		Pagination pagination = new Pagination(1000);

		//TODO 暂时仅同步正向交付的设备，按设备类过滤
		List<AdmM2dEquip> equips = configService.queryM2dEquip(projectId);
		List<String> classCodeList = new ArrayList<>(equips.size());
		for (AdmM2dEquip equip : equips) {
			classCodeList.add(equip.getClassCode());
		}
		if (classCodeList.size() > 0) {
			try {
				String filter = "classCode in " + objectMapper.writeValueAsString(classCodeList);
				params.put("filters", filter);
			} catch (IOException e) {

			}
		}

		return queryAllPage(() -> admClient.queryEquip(groupCode, projectId, appId, user, token, params), params, pagination);
	}

}
