package com.palmnest.service.estateserve.impl;

import com.alibaba.fastjson.JSON;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.Constants;
import com.palmnest.application.dto.OrgKoalaInfoVo;
import com.palmnest.dao.estateserve.VisitorInFoDao;
import com.palmnest.model.estateserve.FaceProjectInfo;
import com.palmnest.model.property.TbOrganization;
import com.palmnest.service.OrganizationManager;
import com.palmnest.service.estateserve.FaceClientManager;
import com.palmnest.service.redis.BaseRedisService;
import com.palmnest.service.ws.FacePlusClient;

import org.apache.http.client.CookieStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Component("faceClientManager")
public class FaceClientManagerImpl implements FaceClientManager {

	
	private Logger LOG = LoggerFactory.getLogger(FaceClientManagerImpl.class);
	
	@Autowired
	private VisitorInFoDao visitorInFoDao;
	
	@Autowired
	private OrganizationManager organizationManager;
	
	@Autowired
	private BaseRedisService baseRedisService;
	
	
	

	@Override
	public void UpdateFaceCookieStore() {
		
		LOG.info("UpdateFaceCookieStore 定时任务 更新CookieStore ===>");
		
		int passType =4;
		List<TbOrganization> listOrgEntirys = organizationManager.passTypeOrgList(passType);
		for (TbOrganization faceProjectInfo : listOrgEntirys) {
			
			
			
			
			CookieStore cookieStore = FacePlusClient.authLogin(faceProjectInfo.getKoalaAuthUrl(),faceProjectInfo.getKoalaUserName(), faceProjectInfo.getKoalaUserPwd());
			if (cookieStore == null) {
				continue;
			}	
			
			OrgKoalaInfoVo orgKoalaInfoVo =new OrgKoalaInfoVo(faceProjectInfo.getKoalaAuthUrl(),faceProjectInfo.getKoalaUserName(),faceProjectInfo.getKoalaUserPwd());
	        baseRedisService.set("User.id:"+ CodeConstants.RSA_REDIS_ORGKOALA_KEY + "_"+faceProjectInfo.getId(),JSON.toJSONString(orgKoalaInfoVo));
			
			Constants.FaceMap.put("org_cookiestore_" + faceProjectInfo.getId(), cookieStore);		
			
		}		
		
		
		
		
		/*List<FaceProjectInfo> list = visitorInFoDao.getFaceProjectInfoList();
		for (FaceProjectInfo faceProjectInfo : list) {
			CookieStore cookieStore = FacePlusClient.authLogin(faceProjectInfo.getAccountNumber(), faceProjectInfo.getAccountPassword());
			if (cookieStore == null) {
				continue;
			}
			if (faceProjectInfo.getType().equals("1")) {
				Constants.FaceMap.put("org_cookiestore_" + faceProjectInfo.getProjectId(), cookieStore);
			} else {
				Constants.FaceMap.put("vis_cookiestore_" + faceProjectInfo.getProjectId(), cookieStore);
			}
		}*/
	}




	@Override
	public CookieStore getCookieStoreByOrgId(Integer orgId) {
		
		LOG.info("getCookieStoreByOrgId 根据OrgId 获得CookieStore ===>");
		
		CookieStore cookieStore = Constants.FaceMap.get("org_cookiestore_" + orgId);
		if(null == cookieStore){
			UpdateFaceCookieStore();//没有cookieStore 在调用下定时任务
		}
		cookieStore = Constants.FaceMap.get("org_cookiestore_" + orgId);
		
		if(null == cookieStore){
			LOG.error("getCookieStoreByOrgId cookieStore 为null 获得CookieStore 导常===>");
		}else{
			LOG.info("getCookieStoreByOrgId 获得 cookieStore===>项目Id:"+orgId);
		}
		
		return cookieStore;
	}

}
