package com.example.action;

import com.example.frame.base.BaseResponse;
import com.example.frame.base.Error;
import com.example.frame.base.ErrorType;
import com.example.frame.base.Token;
import com.example.frame.utils.*;
import com.example.module.admin.ent.NginxCtrl;
import com.example.module.admin.ent.Service;
import com.example.module.admin.mgr.MappingManager;
import com.example.module.admin.req.*;
import com.example.module.admin.rsp.LoginResponse;
import com.example.module.admin.rsp.MappingFindResponse;
import com.example.module.admin.rsp.PortLoadResponse;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import xyz.wbsite.wsqlite.ObjectClient;
import xyz.wbsite.wsqlite.Where;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
public class AjaxController {

    @Autowired
    private MappingManager mappingManager;
    @Autowired
    private NginxCtrl nginxCtrl;
    @Autowired
    private ObjectClient objectClient;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${admin.username}")
    private String username;
    @Value("${admin.password}")
    private String password;

    @RequestMapping("/ajax")
    @ResponseBody
    public BaseResponse ajax(@RequestParam("method") String method, HttpServletRequest request, HttpServletResponse response) {

        BaseResponse baseResponse = new BaseResponse();
        String jsonString = null;
        try {
            if (method == null) {
                baseResponse.addError(new Error(ErrorType.BUSINESS_ERROR, "请求方法不能为空!"));
                return baseResponse;
            }
            Token token = LocalData.getToken();
            if (token == null) {
                token = LocalData.getTempToken();
            }
            if (!token.hasResource(method)) {
                baseResponse.addError(new Error(ErrorType.BUSINESS_ERROR, "无权调用该接口!"));
                return baseResponse;
            }

            InputStreamReader isr = new InputStreamReader(request.getInputStream(), "UTF-8");
            BufferedReader in = new BufferedReader(isr);
            jsonString = in.readLine();

            switch (method) {
                case "ajax.login":
                    baseResponse = login(jsonString, token);
                    break;
                case "nginx.start":
                    baseResponse = nginxStart(jsonString, token);
                    break;
                case "nginx.stop":
                    baseResponse = nginxStop(jsonString, token);
                    break;
                case "nginx.reload":
                    baseResponse = nginxReload(jsonString, token);
                    break;
                // 创建映射
                case "ajax.admin.mapping.create":
                    baseResponse = createMapping(jsonString, token);
                    flushConfig();
                    break;
                // 删除映射
                case "ajax.admin.mapping.delete":
                    baseResponse = deleteMapping(jsonString, token);
                    flushConfig();
                    break;
                // 修改映射
                case "ajax.admin.mapping.update":
                    baseResponse = updateMapping(jsonString, token);
                    flushConfig();
                    break;
                case "ajax.admin.service.load":
                    baseResponse = loadPort(jsonString, token);
                    break;
                case "ajax.admin.config.flush":
                    flushConfig();
                    baseResponse = new BaseResponse();
                    break;
                default:
                    baseResponse.addError(ErrorType.INVALID_PARAMETER, Message.NOT_EXIST_METHOD);
                    break;
            }

        } catch (Exception ex) {
            baseResponse.addError(ErrorType.SYSTEM_ERROR, Message.ERROR_500);
            LogUtil.dumpException(ex);
        } finally {
            if (baseResponse.hasError()) {
                LogUtil.e("请求方法" + method + ", 请求参数：" + jsonString);
                LogUtil.e("返回结果包含异常" + MapperUtil.toJson(baseResponse));
            }
        }
        return baseResponse;
    }

    private BaseResponse login(String jsonString, Token token) {
        LoginRequest loginRequest = MapperUtil.toJava(jsonString, LoginRequest.class);
        LoginResponse loginResponse = new LoginResponse();
        ValidationUtil.validate(loginRequest, loginResponse);
        if (loginResponse.hasError()) {
            return loginResponse;
        }
        if (!loginRequest.getUsername().endsWith(username)) {
            loginResponse.addError(ErrorType.BUSINESS_ERROR, "用户不存在!");
            return loginResponse;
        }

        if (!loginRequest.getPassword().endsWith(password)) {
            loginResponse.addError(ErrorType.BUSINESS_ERROR, "密码错误!");
            return loginResponse;
        }

        String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String encode = MD5Util.encode(yyyyMMdd + username + password);
        LocalData.getResponse().addCookie(new Cookie("token", encode));
        return loginResponse;
    }

    private BaseResponse nginxStart(String jsonString, Token token) {
        BaseResponse baseResponse = new BaseResponse();
        if (nginxCtrl.isRunning()) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序已经运行");
            return baseResponse;
        }
        ProcessUtil.execBat(nginxCtrl.getStartCmd());
        return baseResponse;
    }

    private BaseResponse nginxStop(String jsonString, Token token) {
        BaseResponse baseResponse = new BaseResponse();
        if (!nginxCtrl.isRunning()) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序尚未运行");
            return baseResponse;
        }
        ProcessUtil.execBat(nginxCtrl.getStopCmd());
        return baseResponse;
    }

    private synchronized BaseResponse nginxReload(String jsonString, Token token) {
        BaseResponse baseResponse = new BaseResponse();
        if (!nginxCtrl.isRunning()) {
            baseResponse.addError(ErrorType.BUSINESS_ERROR, "程序尚未运行");
            return baseResponse;
        }
        // 刷新配置文件
        flushConfig();
        // 重新加载配置文件
        ProcessUtil.execBat(nginxCtrl.getReloadCmd());
        return baseResponse;
    }

    /**
     * 创建映射
     */
    private BaseResponse createMapping(String jsonString, Token token) {
        MappingCreateRequest request = MapperUtil.toJava(jsonString, MappingCreateRequest.class);
        return mappingManager.create(request, token);
    }

    /**
     * 删除映射
     */
    private BaseResponse deleteMapping(String jsonString, Token token) {
        MappingDeleteRequest request = MapperUtil.toJava(jsonString, MappingDeleteRequest.class);
        return mappingManager.delete(request, token);
    }

    /**
     * 修改映射
     */
    private BaseResponse updateMapping(String jsonString, Token token) {
        MappingUpdateRequest request = MapperUtil.toJava(jsonString, MappingUpdateRequest.class);
        return mappingManager.update(request, token);
    }

    /**
     * 查询映射
     */
    private BaseResponse findMapping(String jsonString, Token token) {
        MappingFindRequest request = MapperUtil.toJava(jsonString, MappingFindRequest.class);
        return mappingManager.find(request, token);
    }

    private BaseResponse loadPort(String jsonString, Token token) {
        PortLoadResponse response = new PortLoadResponse();
        try {
            List<Service> select = objectClient.select(Service.class, Where.builder().build(), 1, 0);

            for (Service service : select) {
                MappingFindRequest mappingFindRequest = new MappingFindRequest();
                mappingFindRequest.setServiceId(service.getId());
                mappingFindRequest.setPageSize(0);
                MappingFindResponse mappingFindResponse = mappingManager.find(mappingFindRequest, token);
                if (mappingFindResponse.hasError()) {
                    response.addErrors(mappingFindResponse.getErrors());
                    return response;
                }
                service.setMappingList(mappingFindResponse.getResult());
            }
            response.setResult(select);
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            response.addError(ErrorType.BUSINESS_ERROR, "配置加载错误");
            return response;
        }
        return response;
    }

    private synchronized void flushConfig() {
        Writer wr = null;
        try {
            File config = nginxCtrl.getConfig();
            HashMap<String, Object> context = new HashMap<>();

            PortLoadResponse portLoadResponse = (PortLoadResponse) loadPort("", LocalData.getSysToken());
            context.put("services", portLoadResponse.getResult());
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("nginx.conf.ftl");
            wr = new OutputStreamWriter(new FileOutputStream(config), "UTF-8");
            //写入
            template.process(context, wr);
            //关闭流
            wr.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            try {
                wr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
