package com.example.action;

import com.example.module.admin.mgr.MappingManager;
import com.example.module.admin.req.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.example.frame.utils.LocalData;
import com.example.frame.utils.MapperUtil;
import com.example.frame.utils.Message;
import com.example.frame.base.BaseResponse;
import com.example.frame.base.ErrorType;
import com.example.frame.base.Token;
import com.example.frame.utils.LogUtil;
import com.example.frame.base.Error;

@Controller
public class AjaxController {

    @Autowired
    private MappingManager mappingManager;

    @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.example.example":
                    break;
                // 创建映射
                case "ajax.admin.mapping.create":
                    baseResponse = createMapping(jsonString, token);
                    break;
                // 删除映射
                case "ajax.admin.mapping.delete":
                    baseResponse = deleteMapping(jsonString, token);
                    break;
                // 修改映射
                case "ajax.admin.mapping.update":
                    baseResponse = updateMapping(jsonString, token);
                    break;
                // 查询映射
                case "ajax.admin.mapping.find":
                    baseResponse = findMapping(jsonString, token);
                    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 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);
    }

}
