package com.example.demo.module.app;

import com.example.demo.common.GlobalConfig;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.jc.util.JCFileTool;
import pers.jc.util.JCLogger;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 控制器-获取和校验一些App信息
 */
@CrossOrigin
@RestController
@RequestMapping("/app")
public class AppController {

    @GetMapping("/getUserAgreement")
    public String getUserAgreement(HttpServletRequest request) throws Exception {
        String path = JCFileTool.joinPath(JCFileTool.getRootPath(), "res", "用户协议", "国内",
                getFileNameAfterCheckLanguage(request, "UserAgreement", ".txt"));
        return JCFileTool.readStr(new File(path));
    }

    private String getFileNameAfterCheckLanguage(HttpServletRequest request, String fileName, String suffix) {
        String language = request.getParameter("language");
        if (language != null) fileName += "_" + language;
        return fileName + suffix;
    }

    @GetMapping("/cn/privacy-policy")
    public String getPrivacyPolicyCN(HttpServletRequest request) throws Exception {
        String path = JCFileTool.joinPath(JCFileTool.getRootPath(), "res", "隐私政策", "国内", "privacy_policy_cn.txt");
        return  parseToTextOrHtml(new File(path), isFromUnity(request), "青凤鸾隐私政策");
    }

    @GetMapping("/en/privacy-policy")
    public String getPrivacyPolicyEN(HttpServletRequest request) throws Exception {
        return getPrivacyPolicyEN2(request);
    }

    @GetMapping("/international/en/privacy-policy")
    public String getPrivacyPolicyEN2(HttpServletRequest request) throws Exception {
        String path = JCFileTool.joinPath(JCFileTool.getRootPath(), "res", "隐私政策", "国际", "privacy_policy_en.txt");
        return  parseToTextOrHtml(new File(path), isFromUnity(request), "WONDER FITTER Privacy Policy");
    }

    private String parseToTextOrHtml(File file, boolean toText, String title) throws Exception {
        if (toText) {
            return JCFileTool.readStr(file);
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<div>");
        stringBuilder.append("<h1 align=\"center\">" + title + "</h1>");
        for (String line : JCFileTool.readLines(file)) {
            stringBuilder.append("<p>");
            stringBuilder.append(line);
            stringBuilder.append("</p>");
        }
        stringBuilder.append("</div>");
        return stringBuilder.toString();
    }

    boolean debugIsFromUnity = false;
    private boolean isFromUnity(HttpServletRequest request) {
        String unityHeader = request.getHeader("x-unity-version");
        boolean isUnity = unityHeader != null && unityHeader.length() > 0;
        if (debugIsFromUnity) {
            if (isUnity) JCLogger.info("隐私政策请求来自unity");
            else JCLogger.info("隐私政策请求没有unity头部");
            Enumeration<String> headers = request.getHeaderNames();
            while (headers.hasMoreElements())
            {
                String key = headers.nextElement();
                System.out.println("key:" + key);
                System.out.println(request.getHeader(key));
            }
        }
        return isUnity;
    }
}
