package com.bocloud.ims.service.internal;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.BocloudToken;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.common.AccountSecurity;
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 org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * SOC管理员用户访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月5日
 *
 */
@Component
public class SmsManagerService 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<AccountSecurity> secKey(String apiKey) {
		Map<String, Object> params = MapTools.simpleMap("apiKey", apiKey);
		RemoteService remoteService = serviceFactory.build(service, "/v1/users/key", 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, JSONObject.parseObject(data, AccountSecurity.class), "success");
	}

	public BocloudResult<User> detail(Long id) {
		RemoteService remoteService = serviceFactory.build(service, "/v1/users/" + 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, User.class), "success");
	}

	public BocloudResult<User> basic(Long id) {
		RemoteService remoteService = serviceFactory.build(service, "/v1/users/basic/" + 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, User.class), "success");
	}

	public BocloudResult<List<User>> listAdminManager() {
		JSONObject object = new JSONObject();
		object.put(Common.CONDITION, "listAdminmanager");
		RemoteService remoteService = serviceFactory.build(service, "/v1/users/condition", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.CONDITION, object.toJSONString()));
		BocloudResult result = remoteService.invoke();
		if (result.isFailed()) {
			return new BocloudResult<>(false, result.getMessage());
		}
		String data = JSONObject.toJSONString(result.getData());
		return new BocloudResult<>(true, JSONObject.parseArray(data, User.class), "success");
	}

	/**
	 * 按权限菜单查询用户
	 *
	 * @param type
	 * @return
	 */
	public BocloudResult listByPermission(String type) {
		JSONObject object = new JSONObject();
		object.put(Common.CONDITION, "listByPermission");
		object.put("router", type);
		Map<String, Object> paramMap = MapTools.simpleMap(Common.CONDITION, JSONObject.toJSONString(object));
		RemoteService remoteService = serviceFactory.build(service, "/v1/users", BoCloudMethod.GET, headers,
				paramMap);
		return remoteService.invoke();
	}

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

}
