package com.cym.utils;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cym.bean.Page;
import com.cym.ext.AdminExt;
import com.cym.ext.UserExt;
import com.cym.model.User;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

/**
 * Author: D.Yang Email: koyangslash@gmail.com Date: 16/10/9 Time: 下午1:37
 * Describe: 基础控制器
 */
@Component
public class BaseController {
	@Autowired
	public JpaHelper jpaHelper;

	public JsonResult renderError() {
		JsonResult result = new JsonResult();
		result.setSuccess(false);
		result.setStatus("200");
		result.setTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		;
		return result;
	}

	public JsonResult renderError(String msg) {
		JsonResult result = renderError();
		result.setMsg(msg);
		return result;
	}

	public JsonResult renderSuccess() {
		JsonResult result = new JsonResult();
		result.setSuccess(true);
		result.setStatus("200");
		result.setTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		return result;
	}

	public JsonResult renderSuccess(Object obj) {
		JsonResult result = renderSuccess();
		result.setObj(obj);
		return result;
	}

	public AdminExt getAdmin() {
		HttpSession httpSession = getHttpSession();
		if (httpSession != null) {
			AdminExt adminExt = (AdminExt) httpSession.getAttribute("adminLogin");
			return adminExt;
		} else {
			return null;
		}
	}

	public String getAdminId() {
		AdminExt adminExt = getAdmin();
		return adminExt != null ? adminExt.getId() : null;
	}

	public String getLoginIp() {
		HttpSession httpSession = getHttpSession();
		if (httpSession != null) {
			return (String) getHttpSession().getAttribute("loginIp");
		} else {
			return null;
		}
	}

	public String getUserId() {
		User user = getUser();
		return user != null ? user.getId() : null;
	}

	public User getUser() {
		UserExt userExt = (UserExt) getHttpSession().getAttribute("userLogin");
		if (userExt != null) {
			return jpaHelper.findById(userExt.getId(), User.class);
		} else {
			String token = getHttpServletRequest().getHeader("token");
			if (StrUtil.isNotEmpty(token)) {
				return jpaHelper.findOneByQuery(new ConditionAndWrapper().eq(User::getToken, token), User.class);
			}
		}

		return null;
	}

	public Page buildPage(Integer pageNum, Integer pageSize) {
		Page page = new Page<>();
		if (pageNum != null) {
			page.setPageNum(pageNum);
		}
		if (pageSize != null) {
			page.setPageSize(pageSize);
		}
		return page;
	}

	public String removeHttp(String str) {
		if (StrUtil.isNotEmpty(str)) {
			return str.replace("http://", "").replace("https://", "").replace("/", "");
		}

		return str;
	}

	public HttpSession getHttpSession() {
		ServletRequestAttributes requestAttributes = ServletRequestAttributes.class.cast(RequestContextHolder.getRequestAttributes());
		if (requestAttributes != null) {
			HttpServletRequest request = requestAttributes.getRequest();
			HttpSession httpSession = request.getSession();
			return httpSession;
		}
		return null;
	}

	public HttpServletRequest getHttpServletRequest() {
		ServletRequestAttributes requestAttributes = ServletRequestAttributes.class.cast(RequestContextHolder.getRequestAttributes());
		HttpServletRequest request = requestAttributes.getRequest();

		return request;
	}
}
