package com.mossle.internal.store.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mossle.api.tenant.TenantHolder;

import com.mossle.core.export.Exportor;
import com.mossle.core.export.TableModel;
import com.mossle.core.mapper.BeanMapper;
import com.mossle.core.page.Page;
import com.mossle.core.query.PropertyFilter;
import com.mossle.core.spring.MessageHelper;

import com.mossle.internal.store.persistence.domain.StoreApp;
import com.mossle.internal.store.persistence.manager.StoreAppManager;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("store")
public class StoreAppController {
    private StoreAppManager storeAppManager;
    private MessageHelper messageHelper;
    private Exportor exportor;
    private BeanMapper beanMapper = new BeanMapper();
    private TenantHolder tenantHolder;

    @RequestMapping("store-app-list")
    public String list(@ModelAttribute Page page,
            @RequestParam Map<String, Object> parameterMap, Model model) {
        String tenantId = tenantHolder.getTenantId();
        List<PropertyFilter> propertyFilters = PropertyFilter
                .buildFromMap(parameterMap);
        // propertyFilters.add(new PropertyFilter("EQS_tenantId", tenantId));
        page = storeAppManager.pagedQuery(page, propertyFilters);
        model.addAttribute("page", page);

        return "store/store-app-list";
    }

    @RequestMapping("store-app-input")
    public String input(@RequestParam(value = "id", required = false) Long id,
            Model model) {
        if (id != null) {
            StoreApp storeApp = storeAppManager.get(id);
            model.addAttribute("model", storeApp);
        }

        return "store/store-app-input";
    }

    @RequestMapping("store-app-save")
    public String save(@ModelAttribute StoreApp storeApp,
            RedirectAttributes redirectAttributes) {
        String tenantId = tenantHolder.getTenantId();
        Long id = storeApp.getId();
        StoreApp dest = null;

        if (id != null) {
            dest = storeAppManager.get(id);
            beanMapper.copy(storeApp, dest);
        } else {
            dest = storeApp;

            // dest.setTenantId(tenantId);
        }

        storeAppManager.save(dest);
        messageHelper.addFlashMessage(redirectAttributes, "core.success.save",
                "保存成功");

        return "redirect:/store/store-app-list.do";
    }

    @RequestMapping("store-app-remove")
    public String remove(@RequestParam("selectedItem") List<Long> selectedItem,
            RedirectAttributes redirectAttributes) {
        List<StoreApp> storeApps = storeAppManager.findByIds(selectedItem);
        storeAppManager.removeAll(storeApps);
        messageHelper.addFlashMessage(redirectAttributes,
                "core.success.delete", "删除成功");

        return "redirect:/store/store-app-list.do";
    }

    @RequestMapping("store-app-export")
    public void export(@ModelAttribute Page page,
            @RequestParam Map<String, Object> parameterMap,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String tenantId = tenantHolder.getTenantId();
        List<PropertyFilter> propertyFilters = PropertyFilter
                .buildFromMap(parameterMap);
        propertyFilters.add(new PropertyFilter("EQS_tenantId", tenantId));
        page = storeAppManager.pagedQuery(page, propertyFilters);

        List<StoreApp> storeApps = (List<StoreApp>) page.getResult();

        TableModel tableModel = new TableModel();
        tableModel.setName("store info");
        tableModel.addHeaders("id", "name");
        tableModel.setData(storeApps);
        exportor.export(request, response, tableModel);
    }

    // ~ ======================================================================
    @Resource
    public void setStoreAppManager(StoreAppManager storeAppManager) {
        this.storeAppManager = storeAppManager;
    }

    @Resource
    public void setMessageHelper(MessageHelper messageHelper) {
        this.messageHelper = messageHelper;
    }

    @Resource
    public void setExportor(Exportor exportor) {
        this.exportor = exportor;
    }

    @Resource
    public void setTenantHolder(TenantHolder tenantHolder) {
        this.tenantHolder = tenantHolder;
    }
}
