package com.bofeng.controller;

import com.bofeng.JwtHelper;
import com.bofeng.service.IdService;
import com.bofeng.support.BofengProperties;
import com.yvan.Model;
import com.yvan.freemarker.RedirectViewJs;
import com.yvan.mvc.Pd;
import com.yvan.platform.JsonWapper;
import com.yvan.springmvc.HttpUtils;
import com.yvan.springmvc.ResultModel;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

@RestController
public class RootController {

    @Autowired
    private IdService idService;

    @Autowired
    private BofengProperties bofengProperties;

    @GetMapping("/ok")
    public String ok() {
        return "ok";
    }


//    @ApiOperation("获取当前用户信息")
    @GetMapping("/me")
    public Model<ResultModel> me() {
        ResultModel model = new ResultModel()
                .set("userId", JwtHelper.getUserId())
                .set("staffName", JwtHelper.getStaffName())
                .set("expireTime", JwtHelper.getExpireTime());

        return Model.newSuccess(model);
    }

//    @ApiOperation("time")
    @GetMapping("/time")
    public Model<Map<String, Long>> time(@Pd(name = "t1", desc = "客户端时间") Long t1) {
        Map<String, Long> timeRp = new LinkedHashMap<>();
        timeRp.put("t1", t1);
        long start = System.currentTimeMillis();
        //mock select by sql
        //try {
        //    Thread.sleep(10);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        //long dbTime = start;
        long dbTime = idService.getDbTime();
        long cost = System.currentTimeMillis() - start;
        timeRp.put("t2", dbTime);
        timeRp.put("t3", dbTime + cost);
        return Model.newSuccess(timeRp);
    }

//    @ApiOperation("获取当前访问属性")
    @GetMapping("/domain")
    public Model<JsonWapper> domain(HttpServletRequest request) {
        JsonWapper jw = new JsonWapper();

        Enumeration<String> enuma = request.getHeaderNames();
        while (enuma.hasMoreElements()) {
            String key = enuma.nextElement();
            jw.set("HeaderNames", key, request.getHeader(key));
        }

        jw.set("ServerName", request.getServerName());
        jw.set("Host", request.getHeader("Host"));
        jw.set("X-Forwarded-For", request.getHeader("X-Forwarded-For"));
        jw.set("RequestURI", request.getRequestURI());
        jw.set("scheme", request.getScheme());
        jw.set("url", request.getScheme() + "://" + request.getHeader("Host") + request.getRequestURI());

        jw.set("currentHost", HttpUtils.currentHost());
        jw.set("currentToken", HttpUtils.currentToken());
        jw.set("currentRemoteIp", HttpUtils.currentRemoteIp());
        jw.set("currentUrl", HttpUtils.currentUrl());

        jw.set("joda.DateTime", new DateTime());
        jw.set("currentTimeMillis", System.currentTimeMillis());
        jw.set("Date", new Date());
        jw.set("Timestamp", new Timestamp(System.currentTimeMillis()));

        Calendar cal = Calendar.getInstance();
        TimeZone timeZone = cal.getTimeZone();
        jw.set("timeZone.getID", timeZone.getID());
        jw.set("timeZone.getDisplayName", timeZone.getDisplayName());

        return Model.newSuccess(jw);
    }

    @GetMapping("bofeng")
    public Model<BofengProperties> bofengPropertie() {
        return Model.newSuccess(bofengProperties);
    }

    @GetMapping(value = {"/", "/index.html"})
    public ModelAndView index() {
        if (!JwtHelper.isLogined()) {
            return new ModelAndView(new RedirectViewJs("/user/login.html"));
        }

        return new ModelAndView(new RedirectViewJs("/admin/index.html"));
    }
}
