package com.app.admin.framework.index.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.app.admin.framework.system.administrator.model.SysAdministrator;
import com.app.admin.framework.system.administrator.service.SysAdministratorService;
import com.app.admin.framework.system.permission.authorization.service.SysAdministratorRoleService;
import com.app.admin.framework.system.permission.authorization.service.SysRoleModuleService;
import com.app.admin.framework.system.permission.authorization.view.SysAdministratorRoleView;
import com.app.component.sysconfig.service.SysConfigService;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.controller.CrudControllerSupport;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.utils.request.RequestUtil;
import com.app.framework.core.utils.request.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("")
public class IndexController extends CrudControllerSupport {

    @Autowired
    SysConfigService sysConfigService;
    @Autowired
    SysAdministratorService sysAdministratorService;
    @Autowired
    SysAdministratorRoleService sysAdministratorRoleService;
    @Autowired
    SysRoleModuleService sysRoleModuleService;

    /**
     * 用户登录
     */
    @RequestMapping(value = "/")
    public void toIndex(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getRequestDispatcher("/index").forward(request, response);
    }

    /**
     * 用户登录
     */
    @RequestMapping(value = "/login")
    public ModelAndView tologin(HttpServletRequest request) throws Exception {
        JSONObject app = sysConfigService.getJson("Website");
        JSONObject seo = sysConfigService.getJson("SEO");
        String icp = sysConfigService.getString("ICP");
        HashMap<String, Object> map = new HashMap<>();
        map.put("app", app);
        map.put("seo", seo);
        map.put("icp", icp);
        return new ModelAndView("admin/framework/login.html", map);
    }

    @RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request) {
        SysAdministrator loginUser = SessionUtil.getCurrentUser(request);
        JSONObject app = sysConfigService.getJson("Website");
        JSONObject seo = sysConfigService.getJson("SEO");
        String icp = sysConfigService.getString("ICP");
        Map<String, Object> result = new HashMap<>();
        result.put("app", app);
        result.put("seo", seo);
        result.put("icp", icp);
        result.put("userInfo", loginUser);
        if ("style_page".equals(loginUser.getAppStyle())) {
            return new ModelAndView("admin/framework/index/index_page.html", result);
        } else {
            return new ModelAndView("admin/framework/index/index.html", result);
        }
    }

    /**
     * 欢迎页面
     */
    @RequestMapping(value = "/welcome")
    public ModelAndView welcome(HttpServletRequest request) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        List<SysAdministratorRoleView> roleList = sysAdministratorRoleService.selectByUserId(loginUser.getId(), StateEnum.NORMAL);
        Map<String, Object> result = new HashMap<>();
        result.put("userInfo", loginUser);
        result.put("roleList", roleList);
        result.put("loginIp", RequestUtil.getIpAddr(request));
        result.put("lastTime", DateUtil.date().toString());

        result.put("serverIp", request.getLocalAddr());
        result.put("serverPort", request.getLocalPort());
        return new ModelAndView("admin/framework/index/welcome.html", result);
    }

    /**
     * 表单组件示例页面
     */
    @RequestMapping(value = "/form_demo")
    public ModelAndView form_demo(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        return new ModelAndView("admin/demo.html", result);
    }
}
