package cn.hmsoft.skill.helper;

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

import cn.hmsoft.application.AppHelper;
import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.skill.constants.ArtParamType;
import cn.hmsoft.skill.data.dao.cf.enrol.CfEnrolParamDao;
import cn.hmsoft.skill.data.model.cf.enrol.CfEnrolParam;
import cn.hmsoft.frame.constants.FrameStatus;
import cn.hmsoft.frame.data.model.FrameParam;
import cn.hmsoft.frame.util.FrameParamUtil;
import cn.hmsoft.helper.BeanHelper;
import cn.hmsoft.helper.CollectionHelper;
import cn.hmsoft.helper.SecureHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.pay.impl.PayChannelAbstract;
import cn.hmsoft.pay.impl.weixin.WeiXinPayUtil;
import cn.hmsoft.pay.util.PayUtil;
import cn.hmsoft.weixin.data.model.WxDefine;
import cn.hmsoft.weixin.util.WeiXinUtil;

public class ArtParamHelper extends FrameParamUtil {

	public final static String SessionName_EnrolWeixinCode = "EnrolWeixinCode";
	public final static String SessionName_EnrolWeixinUser = "EnrolWeixinUser";
	public final static String SessionName_EnrolStdReg = "EnrolStdReg";

	/*******************
	 * 当前年份
	 */
	private static String ArtShortYear = "";
	/******************
	 * 加载参数密钥
	 */
	public static String ReloadParamPassword = "2020";
	/*******************
	 * 参数加载时长
	 */
	public static int ReloadParamInterval = 10;
	/*******************
	 * 当前年份
	 */
	public static String ArtYear = "2020";

	/*******************
	 * 当前状态
	 */
	public static String ArtStatus = "Config";

	/*******************
	 * 学校代码
	 */
	public static String SchoolCode = "10654";
	/*****************
	 * 学校名称
	 */
	public static String SchoolName = "";
	/*****************
	 * 业务名称
	 */
	public static String BusinessName = "本科校考";

	/*****************
	 * 学校所在省份
	 */
	public static String SchoolProvince = "";

	/*****************
	 * 学校微信地址
	 */
	public static String SchoolWeixinUrl = "";

	/*****************
	 * 学校的公众号信息，必须有
	 */
	public static String SchoolWeixinAppId = "";
	/*****************
	 * 学校的商户编号
	 */
	public static String SchoolMerchantId = "";
	/*****************
	 * 是否启用二级学院
	 */
	public static String TwoLevelCollege = "InActive";

	/******************
	 * 特权ip
	 */
	public static String PrivilegeAddress = "";
	/******************
	 * 报名系统开放时间
	 */
	public static String EnrolOpenTime = "";
	/******************
	 * 报名系统关闭时间
	 */
	public static String EnrolCloseTime = "";
	/******************
	 * 大陆地区行政区划代码最大值
	 */
	public static Integer MainlandMaxProvinceId = 69;

	private static PayChannelAbstract WeiXinPayImpl = null;

	public static PayChannelAbstract GetWeiXinPayImpl() {
		if (WeiXinPayImpl == null) {
			synchronized (ArtParamHelper.class) {
				WeiXinPayImpl = PayUtil.getPayChannelImpl(SchoolMerchantId, WeiXinPayUtil.ChannelId);
			}
		}
		return WeiXinPayImpl;
	}

	/*****************
	 * 默认公众号
	 */
	private static WxDefine SchoolWxDefine = null;

	/******************
	 * 报名app 考生材料本地路径
	 */
	public static String EnrolMaterialLocalPath = "";
	/******************
	 * 报名app 考生材料web路径
	 */
	public static String EnrolMaterialWebPath = "";
	/******************
	 * 视频路径
	 */
	public static String EnrolMaterialVideoPath = "";
	/******************
	 * 本省考点过期后是否允许省外考点
	 */
	public static String EnrolAllowOutsideProvince = "Active";
	/******************
	 * 考生最大专业数量
	 */
	public static int EnrolStdAspectLimit = 5;
	/******************
	 * 考生最大大类数量
	 */
	public static int EnrolStdCategoryLimit = 5;
	/******************
	 * 划线是否只针对大陆地区
	 */
	public static String PlanMainlandOnly = FrameStatus.Active.toString();
	
	/******************
	 * 录制视频最大可以断掉的秒数
	 */
	public static Integer VideoInterview = 30;

	/***************
	 * 获取短年份
	 */
	public static String getShortYear() {
		if (StringHelper.isEmpty(ArtShortYear)) {
			synchronized (ArtParamHelper.class) {
				if (StringHelper.isEmpty(ArtShortYear)) {
					ArtShortYear = ArtYear.substring(0, 2);
				}
			}
		}
		return ArtShortYear;
	}

	/****************
	 * 获取公众号
	 */
	public static WxDefine getSchoolWeixin() {
		if (SchoolWxDefine == null) {
			synchronized (ArtParamHelper.class) {
				if (SchoolWxDefine == null) {
					SchoolWxDefine = WeiXinUtil.getWxDefine(SchoolWeixinAppId);
				}
			}
		}
		return SchoolWxDefine;
	}

	private static Map<String, List<FrameParam>> paramMap = null;

	/**********************
	 * 初始化参数信息
	 */
	public static void instanceParams(boolean refresh) {
		if (paramMap == null || refresh) {
			synchronized (ArtParamHelper.class) {
				paramMap = new HashMap<String, List<FrameParam>>();
				// 全局的
				paramMap.put(ArtParamType.Gobal, new ArrayList<FrameParam>());
				for (FrameParam p : AppHelper.FrameParamArray)
					paramMap.get(ArtParamType.Gobal).add(p);
				for (CfEnrolParam param : SpringHelper.getBean(CfEnrolParamDao.class).listParam(null)) {
					FrameParam p = new FrameParam();
					AppHelper.GobalParamMap.put(param.getParam_name(), param.getParam_value());
					p.setParam_name(param.getParam_name());
					p.setParam_value(param.getParam_value());
					AppHelper.FrameParamArray.add(p);

					if (!paramMap.containsKey(param.getParam_type()))
						paramMap.put(param.getParam_type(), new ArrayList<FrameParam>());
					paramMap.get(param.getParam_type()).add(p);
				}
				BeanHelper.setStaticFields(ArtParamHelper.class, AppHelper.GobalParamMap);
				// LogHelper.info("Art Paramerter Loaded Complete ......");
			}
		}

	}

	/**********************
	 * 获取某一类型的所有参数
	 * 
	 * @param param_type
	 *            参数类型
	 * @return
	 */
	public static List<FrameParam> listParams(String param_type) {
		instanceParams(false);
		if (paramMap.containsKey(param_type))
			return paramMap.get(param_type);
		return new ArrayList<FrameParam>();
	}

	/*******************
	 * 获取下载文件的标准抬头
	 */
	public static String getFileName(String file_name) {
		return SchoolName + ArtYear + BusinessName + "-" + file_name;
	}

	public static <T> T getParamValue(String param_name, T default_value) {
		return CollectionHelper.getParamValue(ArtParamHelper.GobalParamMap, param_name, default_value);
	}

	// 视频请求加密密钥
	public static String VideoDesEncryptKey = "qmhm2020";
	public static String VideoSdkAppId = "1400346097";
	public static String VideoSecretKey = "8767b095b8400c8dd15441e95d1569900fcf39c7aac167482039bcd8fdf27909";
	public static long VideoExpireTime = 604800l;
	
	public static String IpcameraSdkAppId = "1301686682";
	public static String SecretId = "AKIDbx2waYpL7i37v1NSO4GKSK1TfeM4Kszq";
	public static String SecretKey = "qQ0KUP2WmmW8TdPuDPJxMJ1WmkZ8TYPy";

	private static byte[] VideoDesEncryptKeys = null;

	public static byte[] getVideoDesEncryptKey() {
		if (VideoDesEncryptKeys == null) {
			synchronized (ArtParamHelper.class) {
				VideoDesEncryptKeys = SecureHelper.getSecureKey(VideoDesEncryptKey);
			}
		}
		return VideoDesEncryptKeys;
	}

}
