package com.lightinit.hsdataportal.controller.open;
import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.dictionary.*;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.entity.ResourceCatelog;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.*;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by WangYao on 2017/6/6.
 */
@Controller
@RequestMapping(value = {"/open"})
public class OpenHomeController {

    @Autowired
    private IAPIservice apiService ;

    @Autowired
    private IDataService dataService;

    @Autowired
    private IMicroappService microappService;

    @Autowired
    private IResourceCatelogService resourceCatelogService ;

    @Autowired
    private IUserModuleService userModuleService ;

    @RequestMapping(value = {"/", "home.htm"})
    public String index(ModelMap modelMap, HttpServletRequest request, Model model,String searchName) {
        String str = "" ;
        String code = "" ;
        //api查询实体类
        APIOpenSearchModel apiOpenSearchModel = new APIOpenSearchModel() ;
        apiOpenSearchModel.setNotOpenType(DicResourceOpenType.NOT_OPEN);
        apiOpenSearchModel.setPublishState(DicPublishApiState.PUBLISH);
        apiOpenSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        apiOpenSearchModel.setSortType(0);
        //微应用 查询实体类
        MicroappSearchModel microappSearchModel = new MicroappSearchModel() ;
        microappSearchModel.setNotOpenType(DicResourceOpenType.NOT_OPEN);
        microappSearchModel.setPublishState(DicPublishApiState.PUBLISH);
        microappSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        microappSearchModel.setFlag(0);
        //data 查询实体类
        DataSearchModel dataSearchModel = new DataSearchModel() ;
        if (StringUtils.isEmpty(searchName)){
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(1);
        pager.setPageSize(8);
        apiOpenSearchModel.setPage(pager);
        microappSearchModel.setPage(pager);
        dataSearchModel.setPage(pager);
        dataSearchModel.setSortType(0);
        dataSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        // 查询api tag列表
        TagsSearchListOrTagsModel tagsSearchListModels = resourceCatelogService.getTagsSearchList(DicResourceType.API, DicResourceCatelogType.THEME) ;
        model.addAttribute("tagsSearch",tagsSearchListModels) ;
        // 查询微应用 tag列表
        TagsSearchListOrTagsModel tagsSearchList = resourceCatelogService.getTagsSearchList(DicResourceType.MICROAPP,DicResourceCatelogType.THEME) ;
        model.addAttribute("tagsMicSearch",tagsSearchList) ;
        str = "open/home/index" ;
        }else{
            code =  resourceCatelogService.getTagsSearchListByName(DicResourceType.API, DicResourceCatelogType.THEME,searchName) ;
            apiOpenSearchModel.setCatelogThemeCode(code);
            microappSearchModel.setCatelogThemeCode(code);
            dataSearchModel.setCatelogThemeCode(code);
            str = "open/home/searchIndex" ;
        }
        if (code == null){
            apiOpenSearchModel.setResourceName(searchName);
            microappSearchModel.setResourceName(searchName);
            dataSearchModel.setResourceName(searchName);
        }

        //api 查询
        model.addAttribute("apiList",apiService.queryOpenList(apiOpenSearchModel)) ;
        //微应用 查询
        model.addAttribute("microappList",microappService.selectOpenList(microappSearchModel)) ;
        //data 查询
        model.addAttribute("dataList",dataService.getDataIndex(dataSearchModel,1)) ;

        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        model.addAttribute("imageContextPath",imageContextPath) ;

        model.addAttribute("free", DicPublishSettingChargeMethod.FREE) ;

        model.addAttribute("searchName", searchName) ;

        return str;
    }

    @RequestMapping(value="getThemes.action")
    @ResponseBody
    public List<ResourceCatelog>  getThemes(){
        return   resourceCatelogService.getTypeList(DicResourceCatelogType.THEME) ;
    }

    @RequestMapping(value="getDepts.action")
    @ResponseBody
    public List<ResourceCatelog>  getDepts(){
        return   resourceCatelogService.getTypeList(DicResourceCatelogType.SECTOR) ;
    }

    @RequestMapping(value="userInfo.action")
    @ResponseBody
    public UserInfoModel  userInfo(HttpSession session){
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        return   userModuleService.selectUserInfo(loginUser.getId()) ;
    }

    @RequestMapping(value="advancedSearch.htm")
    public ModelAndView advancedSearch(AdvancedSearchModel advancedSearchModel){
        ModelAndView modelAndView = new ModelAndView("open/home/searchIndex") ;
        if (advancedSearchModel.getCatelogTheme()==0){
            advancedSearchModel.setCatelogTheme(null);
        }
        if (advancedSearchModel.getCatelogSector()==0){
            advancedSearchModel.setCatelogSector(null);
        }
        //api查询实体类
        String resourceType = advancedSearchModel.getResourceType() ;
        if (DicResourceType.API.equals(resourceType)||resourceType==null||resourceType.equals("")){
        APIOpenSearchModel apiOpenSearchModel = new APIOpenSearchModel() ;
        apiOpenSearchModel.setNotOpenType(DicResourceOpenType.NOT_OPEN);
        apiOpenSearchModel.setPublishState(DicPublishApiState.PUBLISH);
//        apiOpenSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        apiOpenSearchModel.setSortType(0);
        apiOpenSearchModel.setResourceName(advancedSearchModel.getResourceName());
        apiOpenSearchModel.setCatelogTheme(advancedSearchModel.getCatelogTheme());
        apiOpenSearchModel.setCatelogSector(advancedSearchModel.getCatelogSector());
        apiOpenSearchModel.setResourceIdentity(advancedSearchModel.getResourceIdentity());
        apiOpenSearchModel.setServiceTag(advancedSearchModel.getServiceTag());
        //api 查询
        modelAndView.addObject("apiList",apiService.queryOpenList(apiOpenSearchModel)) ;
        }

        //微应用 查询实体类
        if (DicResourceType.MICROAPP.equals(resourceType)||resourceType==null||resourceType.equals("")){
            MicroappSearchModel microappSearchModel = new MicroappSearchModel() ;
            microappSearchModel.setNotOpenType(DicResourceOpenType.NOT_OPEN);
            microappSearchModel.setPublishState(DicPublishApiState.PUBLISH);
//        microappSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
            microappSearchModel.setFlag(0);
            microappSearchModel.setResourceName(advancedSearchModel.getResourceName());
            microappSearchModel.setCatelogTheme(advancedSearchModel.getCatelogTheme());
            microappSearchModel.setCatelogSector(advancedSearchModel.getCatelogSector());
            microappSearchModel.setResourceIdentity(advancedSearchModel.getResourceIdentity());
            microappSearchModel.setServiceTag(advancedSearchModel.getServiceTag());
            //微应用 查询
            modelAndView.addObject("microappList",microappService.selectOpenList(microappSearchModel)) ;
        }

        //data 查询实体类
        if (DicResourceType.DATA.equals(resourceType)||resourceType==null||resourceType.equals("")){
            DataSearchModel dataSearchModel = new DataSearchModel();
            dataSearchModel.setSortType(0);
//        dataSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
            dataSearchModel.setResourceName(advancedSearchModel.getResourceName());
            dataSearchModel.setCatelogTheme(advancedSearchModel.getCatelogTheme());
            dataSearchModel.setCatelogSector(advancedSearchModel.getCatelogSector());
            dataSearchModel.setResourceIdentity(advancedSearchModel.getResourceIdentity());
            dataSearchModel.setServiceTag(advancedSearchModel.getServiceTag());
            //data 查询
            modelAndView.addObject("dataList", dataService.getDataIndex(dataSearchModel, 1));
        }
        //查找主题
        modelAndView.addObject("themes",resourceCatelogService.getTypeList(DicResourceCatelogType.THEME)) ;
        //查找部门
        modelAndView.addObject("depts",resourceCatelogService.getTypeList(DicResourceCatelogType.SECTOR)) ;
        return modelAndView ;
    }
}
