package com.example.demo.module.app;

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 javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 控制器-获取和校验一些App信息
 */
@CrossOrigin
@RestController
@RequestMapping("/app")
public class AppController {
    JCFileTool fileTool = new JCFileTool();
    String rootPath = null;

    public AppController() {
        try {
            rootPath = new File("").getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/checkAppVersion")
    public boolean checkAppVersion(String appVersion) throws Exception {
        String path = rootPath
                + File.separator + "res"
                + File.separator + "AppVersion.txt";
        String serverAppVersion = fileTool.readStr(new File(path));
        return serverAppVersion.equals(appVersion);
    }

    @GetMapping("/getUserAgreement")
    public String getUserAgreement(HttpServletRequest request) throws Exception {
        String path = rootPath + File.separator + "res" + File.separator
                + getFileNameAfterCheckLanguage(request, "UserAgreement", ".txt");
        return fileTool.readStr(new File(path));
    }

    @GetMapping("/getPrivacyAgreement")
    public String getPrivacyAgreement(HttpServletRequest request) throws Exception {
        String path = rootPath + File.separator + "res" + File.separator
                + getFileNameAfterCheckLanguage(request, "PrivacyAgreement", ".txt");
        return fileTool.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 userAgent = request.getHeader("User-Agent").toLowerCase();
        String path = rootPath + File.separator
                + "res" + File.separator
                + "google" + File.separator
                + "privacy_policy_cn.txt";
        return  parseToTextOrHtml(new File(path), userAgent.contains("unitywebrequest"), "HOUYI隐私政策");
    }

    @GetMapping("/en/privacy-policy")
    public String getPrivacyPolicyEN(HttpServletRequest request) throws Exception {
        String userAgent = request.getHeader("User-Agent").toLowerCase();
        String path = rootPath + File.separator
                + "res" + File.separator
                + "google" + File.separator
                + "privacy_policy_en.txt";
        return  parseToTextOrHtml(new File(path), userAgent.contains("unitywebrequest"), "HOUYI Privacy Policy");
    }

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