package com.lightinit.hsdataplatform.controller;

import com.lightinit.hsdataplatform.common.CharEncoding;
import com.lightinit.hsdataplatform.common.HttpTookitUtils;
import com.lightinit.hsdataplatform.common.SolrUtils;
import com.lightinit.hsdataplatform.json.pojo.JsonResult;
import com.lightinit.hsdataplatform.json.pojo.ListResult;
import com.lightinit.hsdataplatform.json.pojo.solr.*;
import com.lightinit.hsdataplatform.entity.Solrserver;
import com.lightinit.hsdataplatform.service.ISolrService;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.*;

/**
 * Created by Mr.Yao on 2017/11/2.
 */
@Controller
@RequestMapping("/solr")
public class SolrController {
//    @Autowired
//    private HttpSolrClient httpSolrClient;
    @Autowired
    private ISolrService solrService;

    @RequestMapping(value = "view/broker.action",method = RequestMethod.GET)
    public String print_solr_view_broker(){
        return "solr/server";
    }

    @ResponseBody
    @RequestMapping(value = "json/server.action",method = RequestMethod.GET)
    public ListResult<Solrserver> print_JSON_Solr_Server(@RequestParam(defaultValue = "") String keyword) {
        ListResult<Solrserver> result = new ListResult<Solrserver>();
        List<String> list=new ArrayList<String>();
        result.setRows(solrService.querySolrServer(keyword));
        if (result.getRows()!=null){
            result.setTotal(result.getRows().size());
        }else{
            result.setTotal(0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "json/admin/core/detail.action",method = RequestMethod.GET)
    public CoreOverViewItemEntity print_json_solr_admin_core_detail(@RequestParam(defaultValue = "http://192.168.107.112:8080/solr") String service, @RequestParam(defaultValue = "") String core) {
        CoreOverViewItemEntity result = new CoreOverViewItemEntity();
        if (StringUtils.isEmpty(service) || StringUtils.isEmpty(core))
            return result;
        CoreOverViewResponseEntity item = SolrUtils.parse(service, core);
        if (item != null && item.getIndex() != null) {
            result = item.getIndex();
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "json/admin/cores.action",method = RequestMethod.GET)
    public ListResult<CoreItemDetailEntity> print_json_solr_admin_cores(@RequestParam(defaultValue = "") String service) {
        ListResult<CoreItemDetailEntity> result = new ListResult<CoreItemDetailEntity>();
        if(StringUtils.isEmpty(service))
            return result;
        List<CoreItemDetailEntity> mList=new ArrayList<CoreItemDetailEntity>();
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("wt","json"));
        nameValuePairs.add(new BasicNameValuePair("indexInfo","false"));
        nameValuePairs.add(new BasicNameValuePair("_",String.valueOf(System.currentTimeMillis())));
        String responseBody= HttpTookitUtils.doGet(service+"/admin/cores",nameValuePairs, CharEncoding.UTF8,"application/json");
        if(StringUtils.isEmpty(responseBody)==false) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                CoreResponseEntity item = objectMapper.readValue(responseBody, CoreResponseEntity.class);
                if(item!=null && item.getStatus()!=null) {
                    Iterator<Map.Entry<String, CoreItemEntity>> mIterator = item.getStatus().entrySet().iterator();
                    while (mIterator.hasNext()) {
                        Map.Entry<String, CoreItemEntity> entry = mIterator.next();
                        CoreItemDetailEntity itemDetailEntity=new CoreItemDetailEntity(entry.getValue());
                        CoreOverViewResponseEntity overviewItem = SolrUtils.parse(service, itemDetailEntity.getName());
                        if (overviewItem != null && overviewItem.getIndex() != null) {
                            CoreOverViewItemEntity coreOverViewItemEntity = overviewItem.getIndex();
                            itemDetailEntity.setDeletedDocs(coreOverViewItemEntity.getDeletedDocs());
                            itemDetailEntity.setLastModified(coreOverViewItemEntity.getLastModified());
                            itemDetailEntity.setMaxDoc(coreOverViewItemEntity.getMaxDoc());
                            itemDetailEntity.setNumDocs(coreOverViewItemEntity.getNumDocs());
                            itemDetailEntity.setSegmentCount(coreOverViewItemEntity.getSegmentCount());
                        }
                        mList.add(itemDetailEntity);
                    }
                }
            } catch (JsonParseException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result.setRows(mList);
        if (result.getRows()!=null){
            result.setTotal(result.getRows().size());
        }else{
            result.setTotal(0);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "json/admin/core/dataimport/refresh.action",method = RequestMethod.GET)
    public JsonResult print_json_solr_admin_core_dataimport_refresh(@RequestParam String service, @RequestParam String core, @RequestParam String entity) {
        JsonResult result = new JsonResult();
        if(StringUtils.isEmpty(service)) {
            result.setStatus(-1);
            result.setMessage("参数错误");
            return result;
        }
        List<CoreItemDetailEntity> mList=new ArrayList<CoreItemDetailEntity>();
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("wt","json"));
        nameValuePairs.add(new BasicNameValuePair("command","status"));
        nameValuePairs.add(new BasicNameValuePair("indent","true"));
        nameValuePairs.add(new BasicNameValuePair("_",String.valueOf(System.currentTimeMillis())));
        String responseBody= HttpTookitUtils.doGet(service+"/"+core+"/dataimport",nameValuePairs, CharEncoding.UTF8,"application/json");
        result.setStatus(1);
        result.setMessage(responseBody);
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "json/admin/core/dataimport.action",method = RequestMethod.GET)
    public JsonResult print_json_solr_admin_core_dataimport(@RequestParam String service,@RequestParam String core,@RequestParam String entity) {
        JsonResult result = new JsonResult();
        if(StringUtils.isEmpty(service)){
            result.setStatus(-1);
            result.setMessage("参数错误");
            return result;
        }
        List<CoreItemDetailEntity> mList=new ArrayList<CoreItemDetailEntity>();
        List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("wt","json"));
        nameValuePairs.add(new BasicNameValuePair("command","full-import"));
        nameValuePairs.add(new BasicNameValuePair("clean","true"));
        nameValuePairs.add(new BasicNameValuePair("commit","true"));
        nameValuePairs.add(new BasicNameValuePair("entity",entity));
        nameValuePairs.add(new BasicNameValuePair("verbose","false"));
        nameValuePairs.add(new BasicNameValuePair("optimize","false"));
        nameValuePairs.add(new BasicNameValuePair("debug","false"));
        nameValuePairs.add(new BasicNameValuePair("indent","true"));
        nameValuePairs.add(new BasicNameValuePair("_",String.valueOf(System.currentTimeMillis())));
        String responseBody= HttpTookitUtils.doPost(service+"/"+core+"/dataimport",nameValuePairs, CharEncoding.UTF8,"application/json");
        result.setStatus(1);
        result.setMessage(responseBody);
        return result;
    }
}