package com.bocloud.ims.service.internal;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.common.Tenant;
import com.bocloud.ims.entity.common.TenantPool;
import com.bocloud.service.core.CurrentService;
import com.bocloud.service.core.Service;
import com.bocloud.service.http.core.ServiceFactory;
import com.bocloud.service.http.model.RemoteService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SOC租户内部访问
 *
 * @author wangyu
 * @version 1.0
 * @since 2018年1月4日
 */
@Component
public class SmsTenantService implements InitializingBean {

	@Autowired
	private ServiceFactory serviceFactory;
	@Autowired
	private CurrentService currentService;
	private static Service service = Service.create(BoCloudService.SMS);
	private Map<String, Object> headers = new HashMap<>(4);

	public BocloudResult<Tenant> detail(Long id) {
		RemoteService remote = serviceFactory.build(service, "/v1/tenants/" + id, BoCloudMethod.GET, headers, null);
		BocloudResult result = remote.invoke();
		if (result.isFailed()) {
			return new BocloudResult<>(false, result.getMessage());
		}
		String data = JSONObject.toJSONString(result.getData());
		return new BocloudResult<>(true, JSONObject.parseObject(data, Tenant.class), "success");
	}

	public List<Tenant> listTenantsByIds(List<Long> ids) {
		ArrayList<Param> params=null;
		if (null!=ids && !ids.isEmpty()){
			String idString = JSONArray.toJSONString(ids);
			params = Lists.newArrayList(new Param(MapTools.simpleMap("id", idString.substring(1,idString.length()-1)), Sign.IN));
		}
		Map<String, Object> paramMap = MapTools.simpleMap("simple", false);
		paramMap.put("page", 1);
		paramMap.put("rows", Integer.MAX_VALUE);
		paramMap.put(Common.PARAMS, JSONArray.toJSONString(params));
		RemoteService remoteService = serviceFactory.build(service, "/v1/tenants" , BoCloudMethod.GET, headers, paramMap);
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return new ArrayList<>();
		}
		String data = JSONObject.toJSONString(result.getData());
		return JSONObject.parseObject(data).getJSONArray("rows").toJavaList(Tenant.class);
	}

	public BocloudResult checkPoolGroup(Long vendorId) {
		Map<String, Object> params = Maps.newHashMap();
		JSONObject object = new JSONObject();
		object.put("condition", "checkPoolGroup");
		object.put("vendorId", vendorId);
		params.put(Common.CONDITION, object);
		RemoteService remote = serviceFactory.build(service, "/v1/tenants", BoCloudMethod.GET, headers, params);
		return remote.invoke();
	}

	public BocloudResult<List<Tenant>> listChildren(Long parentId) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("parentId", parentId);
		RemoteService remoteService = serviceFactory.build(service, "/v1/tenants/" + parentId + "/children",
				BoCloudMethod.GET, headers, params);
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return new BocloudResult<>(false, result.getMessage());
		}
		String data = JSONObject.toJSONString(result.getData());
		return new BocloudResult<>(true, JSONArray.parseArray(data, Tenant.class), "success");
	}

	public BocloudResult<List<Tenant>> listAll(List<Param> paramList) {
		Map<String, Object> params = Maps.newHashMap();
		params.put(Common.PAGE, 1);
		params.put(Common.ROWS, Integer.MAX_VALUE);
		params.put(Common.SIMPLE, false);
		params.put(Common.PARAMS, JSONObject.toJSONString(paramList));
		RemoteService remoteService = serviceFactory.build(service, "/v1/tenants", BoCloudMethod.GET, headers, params);
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return new BocloudResult<>(false, result.getMessage());
		}
		String data = JSONObject.toJSONString(result.getData());
		GridBean gridBean = JSONObject.parseObject(data, GridBean.class);
		return new BocloudResult<>(true,
				JSONArray.parseArray(JSONObject.toJSONString(gridBean.getRows()), Tenant.class), "success");
	}

	/**
	 * 根据租户ID查询租户资源池
	 *
	 * @param tenantId
	 * @return
	 */
	public List<TenantPool> listPoolByTenant(Long tenantId) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("tenantId", tenantId);
		RemoteService remoteService = serviceFactory.build(service, "/v1/tenants/" + tenantId + "/pools",
				BoCloudMethod.GET, headers, params);
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return new ArrayList<>();
		}
		String data = JSONObject.toJSONString(result.getData());
		return JSONArray.parseArray(data, TenantPool.class);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.headers.put(BocloudToken.INTERNAL_TOKEN, currentService.getToken());
		this.headers.put("source", currentService.getService().getName());
	}

}
