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.model.BocloudResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.common.Business;
import com.bocloud.ims.entity.common.BusinessPool;
import com.bocloud.ims.entity.common.User;
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 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;

/**
 * @author wangyu
 * @version 4.1.0-SNAPSHOT
 * @since 2018/12/25
 */
@Component
public class SmsBusinessService 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 deleteByPoolId(Long poolGroupId) {
		RemoteService remote = serviceFactory.build(service, "/v1/bizs/pools", BoCloudMethod.DELETE, headers,
				MapTools.simpleMap("poolGroupId", poolGroupId));
		return remote.invoke();
	}

	public List<BusinessPool> listByPoolGroupId(Long poolGroupId) {
		JSONObject object = new JSONObject();
		object.put(Common.CONDITION, "listByPoolGroupId");
		object.put("poolGroupId", poolGroupId);
		RemoteService remote = serviceFactory.build(service, "/v1/bizs/condition", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.CONDITION, object.toJSONString()));
		BocloudResult result = remote.invoke();
		if (result.isFailed()) {
			return new ArrayList<>();
		}
		return JSONArray.parseArray(JSONArray.toJSONString(result.getData()), BusinessPool.class);
	}

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

	public List<Business> listBusinesssByIds(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/bizs" , 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(Business.class);
	}

	public Business getByProjectId(Long projectId) {
		RemoteService remoteService = serviceFactory.build(service, "/v1/projects/" + projectId + "/business",
				BoCloudMethod.GET, headers, null);
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return null;
		}
		String data = JSONObject.toJSONString(result.getData());
		return JSONObject.parseObject(data, Business.class);
	}

	public List<BusinessPool> listByBusinessId(Long businessId) {
		RemoteService remote = serviceFactory.build(service, "/v1/bizs/" + businessId + "/pools", BoCloudMethod.GET,
				headers, null);
		BocloudResult result = remote.invoke();
		if (result.isFailed()) {
			return new ArrayList<>();
		}
		return JSONArray.parseArray(JSONArray.toJSONString(result.getData()), BusinessPool.class);
	}

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