/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.common.filter;


import org.apache.commons.lang3.StringUtils;
import org.springblade.core.tool.utils.AesUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 演示过滤器
 *
 * @author Chill
 */
@Component
public class PreviewFilter implements Filter {

	@Value("${blade.api.crypto.aes-key}")
	private String aesKey;


	private static final List<String> KEYS = new ArrayList<>();
	private static final List<String> KEYS1 = new ArrayList<>();
	static {
		KEYS1.add("notice");
		KEYS1.add("process");
		KEYS1.add("work");
		KEYS1.add("token");
		KEYS1.add("endpoint");
	}
	static {
//		KEYS.add("glinfo");
//		KEYS.add("oauth/token");
//		KEYS.add("/blade-sign/Sign/save");
//		KEYS.add("/blade-sign/Sign/page");
//		KEYS.add("/blade-info/info/submit");
//		KEYS.add("/blade-info/info/update");
//		KEYS.add("/blade-info/info/page");
//		KEYS.add("/blade-info/info/detail");
//		KEYS.add("/blade-info/info/selectGzlx1");
//		KEYS.add("/blade-info/info/selectGzlx2");
//		KEYS.add("/blade-info/info/selectGzlx3");
//		KEYS.add("/blade-JlrhJws/JlrhJws/submit");
//		KEYS.add("/blade-JlrhJws/JlrhJws/update");
//		KEYS.add("/blade-JlrhJws/JlrhJws/getJwsDept");
//		KEYS.add("/blade-JlrhJws/JlrhJws/page");
//		KEYS.add("/blade-JlrhJws/JlrhJws/detail");
//		KEYS.add("/csInfo/info/page");
//		KEYS.add("/csInfo/info/save");
//		KEYS.add("/csInfo/info/getOverDue");
//		KEYS.add("/csInfo/info/update");
//		KEYS.add("/csjcInfo/info/getPageByCsId");
//		KEYS.add("/csjcInfo/info/getXfbm");
//		KEYS.add("/csjcInfo/info/submit");
//		KEYS.add("/csjcInfo/info/update");
//		KEYS.add("/csjcInfo/info/getZgList");
//		KEYS.add("/csjcInfo/info/detail");
//		KEYS.add("/blade-ldInfo/LdInfo/ldWithNoXxy");
//		KEYS.add("/blade-xxy/Xxy/submit");
//		KEYS.add("/appmenu/list");
//		KEYS.add("/appmenu/getDeptList");
//		KEYS.add("/appmenurole/listSbCd");
//		KEYS.add("/appmenu/listFileBase64");
//		KEYS.add("/blade-desk/notice/newList");
//		KEYS.add("/blade-desk/notice/detail");
//		KEYS.add("/blade-mdjfdj/mdjfdj/selectJflx");
//		KEYS.add("/blade-mdjfdj/mdjfdj/mdjfDj");
//		KEYS.add("/blade-yjjl/yjjl/save");
//		KEYS.add("/blade-mdjftj/mdjftj/save");
//		KEYS.add("/blade-rk/rk/save");
//		KEYS.add("/blade-rk/rk/update");
//		KEYS.add("/blade-rk/rk/getXqList");
//		KEYS.add("/blade-rk/rk/getLdList");
//		KEYS.add("/blade-rk/rk/page");
//		KEYS.add("/blade-mdjfdj/mdjfdj/page");
//		KEYS.add("/blade-mdjfdj/mdjfdj/detailById");
//		KEYS.add("/blade-mdjftj/mdjftj/page");
//		KEYS.add("/blade-info/info/getPcsList");
//		KEYS.add("/blade-info/info/getMjList");
//		KEYS.add("/blade-info/info/getXmList");
//		KEYS.add("/pfInfo/info/saveBatch");
//		KEYS.add("/pfInfo/info/page-app");
//		KEYS.add("/blade-glinfo/glinfo/getAppByUserRole");
//		KEYS.add("/blade-sign/Sign/getDkfw");
//		KEYS.add("/csInfo/info/getPermission");
//		KEYS.add("/blade-mdjfdj/mdjfdj/page-app");
//		KEYS.add("/blade-rwxffkb/rwxffkb/page");
//		KEYS.add("/blade-rwxffkb/rwxffkb/page");
//		KEYS.add("/blade-rwxffkb/rwxffkb/update");
//		KEYS.add("/blade-mdjfdj/mdjfdj/addHuiFang");
//		KEYS.add("/blade-mdjfdj/mdjfdj/update");
//		KEYS.add("/blade-mdjfry/mdjfry/update");
//		KEYS.add("/blade-system/user/detailByDhhm1");
//		KEYS.add("/blade-system/user/update");
//		KEYS.add("/blade-jgInfo/jgInfo/getJgList-app");
	}

	@Override
	public void init(FilterConfig filterConfig) {
	}


	@Override
	public void doFilter(ServletRequest servletRequest,  ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;

		HttpServletResponse httpResponse=(HttpServletResponse)servletResponse;
		String path = httpServletRequest.getServletPath();
		String method = httpServletRequest.getMethod();
		String phone = httpServletRequest.getHeader("phone");
		if(StringUtils.isNotEmpty(phone)){
			if (phone.equals("phone") && KEYS.stream().anyMatch(path::contains) ) {

				MyHttpRequest requestWrapper = new MyHttpRequest((HttpServletRequest) servletRequest);
				String encryptData = requestWrapper.getBody();
//			if (requestWrapper.getMethod().equals(HttpMethod.POST.name()) && !path.contains("oauth/token") && !path.contains("blade-sign")) {
//				System.out.println(servletRequest.getContentType());
////				String encryptData = requestWrapper.getBody();
//				byte[] bytes = AesUtil.decryptFormBase64(encryptData, aesKey);
//				requestWrapper = new MyHttpRequest(requestWrapper, bytes);
//				filterChain.doFilter(requestWrapper, httpResponse);
//			}else {
//				filterChain.doFilter(servletRequest, servletResponse);
				//返回数据加密
				ResponseWrapper responseWrapper = new ResponseWrapper(httpResponse);
				filterChain.doFilter(requestWrapper, responseWrapper);
				byte[] responseData = responseWrapper.getResponseData();
				String encryptBASE64 =AesUtil.encryptToBase64(new String(responseData),aesKey);
				PrintWriter writer = httpResponse.getWriter();
				writer.print(encryptBASE64);
				writer.flush();
				writer.close();
//			}

			}
		}
		 else {
			filterChain.doFilter(servletRequest, servletResponse);
//			String get = "GET";
//			if (method.equals(get) || KEYS1.stream().anyMatch(path::contains)) {
//				filterChain.doFilter(servletRequest, servletResponse);
//			} else {
//				throw new RuntimeException("演示环境暂时无法操作！");
//			}
		}
	}

	@Override
	public void destroy() {
	}
}
