package me.chyxion.tigon.codegen.controller;

import java.util.Map;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
import javax.validation.Valid;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.StringUtils;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import me.chyxion.tigon.codegen.service.CodeGenService;
import me.chyxion.tigon.codegen.service.CodeGenBaseTool;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;

/**
 * @version 0.0.1
 * @since 0.0.1
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * Oct 5, 2014 9:34:54 PM
 */
@Controller
@RequestMapping("/codegen")
public class CodeGenController {
    @Autowired
    private CodeGenService service;
    @Autowired
    private CodeGenBaseTool tool;
    @Value("${codegen.components:}")
    private String genComponents;

    @RequestMapping(method = GET)
    public ModelAndView index() {
        return new ModelAndView("webapp/views/codegen")
            .addObject("pkg", tool.getPkg())
            .addObject("components",
                StringUtils.isNotBlank(genComponents) ?
                JSON.toJSONString(genComponents.split("\\s*[,;]\\s*")) : "[]");
    }

    @RequestMapping(value = "/tables")
    public List<Map<String, Object>> tables() {
        return service.tables();
    }

    @RequestMapping(method = POST)
    public boolean gen(@Valid GenForm form) {
        service.process(form.getModule(), form.getModel(), 
                form.getJaColumns(), form.getTable(), form);
        return true;
    }

    @RequestMapping("/list")
    public List<?> list() {
        return service.list();
    }

    @RequestMapping(method = DELETE)
    public boolean delete(
        @RequestParam("items") 
        String items, 
        @RequestParam(value = "dropTable", defaultValue = "true") 
        boolean dropTable) {
        service.delete(JSON.parseArray(items), dropTable);
        return true;
    }

    @Getter
    @Setter
    public static class GenForm {
        @NotNull
        private String columns;
        @NotBlank
        private String model;
        @NotBlank
        private String table;
        private String module;
        private String pkg;
        private boolean genController;
        private boolean genService;
        private boolean genMapper;
        private boolean genModel;
        private boolean genTable;
        private boolean genView;
        private boolean createTable;

        /**
         * @return the columns
         */
        public JSONArray getJaColumns() {
            return JSON.parseArray(columns);
        }
        /**
         * @return the module
         */
        public String getModule() {
            return StringUtils.isNotBlank(module) ? module : "";
        }
    }
}
