package com.lovenav.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.lovenav.dao.UserDao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.service.UrlCateListService;
import com.lovenav.service.UrlListService;
import com.lovenav.vo.CateAndUrl;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/UrlAndCate")
public class UrlAndCateController {

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    UrlCateListService urlCateListService;

    @Autowired
    UrlListService urlListService;
    @Autowired
    UserDao userDao;
    //处理JSON
    @RequestMapping("/disposeJson")
    public String disposeJson(@RequestBody String data2 ,String email)
    {
        String jsonString;
        //先转换成ObjectMapper类型
        ObjectNode objectNode = objectMapper.createObjectNode();
        try {
            JsonNode rootNode = objectMapper.readTree(data2);
            disposeBookmarkFunction1(rootNode,"top",email);
            countCateContainUrlNumber(email);
            HashMap<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("msg", "查询成功");
            jsonString = JSONObject.toJSONString(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return jsonString;
    }
    public void disposeBookmarkFunction1(JsonNode rootNode,String parent,String email)
    {

        for(int i=0;i<rootNode.size();i++)
        {
            JsonNode sonNode = rootNode.get(i);
            if(String.valueOf(sonNode.get("folder")).equals("true")){
                urlCateListService.selectAndInsertUrlCate(email,String.valueOf(sonNode.get("name")),parent);
                JsonNode children = sonNode.get("children");
                disposeBookmarkFunction1(children, String.valueOf(sonNode.get("name")),email);

            }else{
                String name = String.valueOf(sonNode.get("name"));
                String url = String.valueOf(sonNode.get("url"));
                String adddata = String.valueOf(sonNode.get("adddata"));
                urlListService.selectCateAndInsertUrl(parent,name,"123",url,email);
                System.out.println(name +"---" + url + "---" + adddata  + "---" + parent );
            }

        }

        return ;
    }



    //处理成JSON
    @RequestMapping("/disposeBookmarkToJson")
    public String  disposeBookmarkToJson(String userId)
    {
        List<UrlCateList> urlCateLists = urlCateListService.selectUrListByUserId(Integer.valueOf(userId));
        List<UrlList> urlLists = urlListService.selectUrList();
        List<CateAndUrl> cateAndUrlList = new ArrayList<>();
        for (UrlCateList urlCateList  : urlCateLists) {
            CateAndUrl cateAndUrl = new CateAndUrl();
            cateAndUrl.setFloder("true");
            cateAndUrl.setName(urlCateList.getName());
            cateAndUrl.setParentId(urlCateList.getRootCateId());
            cateAndUrl.setCateId(urlCateList.getId());
            cateAndUrlList.add(cateAndUrl);
        }
        for( int i = 0 ; i < urlLists.size() ; i++){
            UrlList urlList = urlLists.get(i);
            if(urlList.getStatus() == 0 ) continue;
            for(CateAndUrl cateAndUrl1 : cateAndUrlList) {
                if (urlList.getCateId() == cateAndUrl1.getCateId())
                {
                    CateAndUrl cateAndUrl = new CateAndUrl();
                    cateAndUrl.setFloder("false");
                    cateAndUrl.setName(urlList.getName());
                    cateAndUrl.setParentId(urlList.getCateId());
                    cateAndUrl.setUrlId(urlList.getId());
                    cateAndUrl.setUrl(urlList.getUrl());
                    cateAndUrl.setIcon(urlList.getIcon());
                    cateAndUrl.setCreatetime(urlList.getCreatetime());
                    cateAndUrl.setViews(urlList.getViews());
                    cateAndUrl.setTags(urlList.getTags());
                    cateAndUrl.setWeigh(urlList.getWeigh());
                    cateAndUrl.setStatus(urlList.getStatus());
                    cateAndUrl.setDesc(urlList.getDesc());
                    cateAndUrl.setNeedLogin(urlList.getNeedLogin());
                    cateAndUrl.setAgentHint(urlList.getAgentHint());
                    cateAndUrl.setIsNeedAgent(urlList.getIsNeedAgent());
                    cateAndUrl.setIsAd(urlList.getIsAd());
                    cateAndUrl.setIsTop(urlList.getIsTop());
                    cateAndUrl.setIsEncrypt(urlList.getIsEncrypt());
                    cateAndUrl1.getChildUC().add(cateAndUrl);
                    break;
                }
            }




        }
        ObjectMapper mapper = new ObjectMapper();
        String result = null;
        try {
            HashMap<String, Object> data = new HashMap<>();
            data.put("code", 200);
            data.put("msg", "处理成功");
            data.put("data", cateAndUrlList);
            result = mapper.writeValueAsString(cateAndUrlList);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return result;

    }

    @RequestMapping("/disposeBookmarkExhibitedToJson")
    public String  disposeBookmarkExhibitedToJson()
    {

        List<UrlList> urlLists = urlListService.selectUrListByNeedLogin();
        List<CateAndUrl> cateAndUrlList = new ArrayList<>();
        Set<String> parentSet = new HashSet<>();
        for( int i = 0 ; i < urlLists.size() ; i++)
        {
            String parentString = urlCateListService.selectUrListCateByUrlCateId(urlLists.get(i).getCateId());
            String [] parentList = parentString.split(",");
            System.out.println(parentString);
            for(String parent : parentList)
            {
                if(parent.equals("0")){
                    continue;
                }
                parentSet.add(parent);
            }
        }

        for (String str : parentSet) {
            UrlCateList urlCateList =urlCateListService.selectByPrimaryKey(Integer.valueOf(str));
            CateAndUrl cateAndUrl = new CateAndUrl();
            cateAndUrl.setFloder("true");
            cateAndUrl.setName(urlCateList.getName());
            cateAndUrl.setParentId(urlCateList.getRootCateId());
            cateAndUrl.setCateId(urlCateList.getId());
            cateAndUrlList.add(cateAndUrl);
        }
//        for( int i = 0 ; i < urlLists.size() ; i++)
//        {
//            CateAndUrl cateAndUrl = new CateAndUrl();
//            cateAndUrl.setFloder("false");
//            cateAndUrl.setName(urlLists.get(i).getName());
//            cateAndUrl.setParentId(urlLists.get(i).getCateId());
//            cateAndUrl.setUrlId(urlLists.get(i).getId());
//            cateAndUrl.setUrl(urlLists.get(i).getUrl());
//            cateAndUrlList.add(cateAndUrl);
//        }

        System.out.println(cateAndUrlList);
        List<CateAndUrl> parentsList = new ArrayList<>();
        //声明返回集合
        List<CateAndUrl> resultList = new ArrayList<>();
        //对全部数据进行遍历
        for (CateAndUrl disease : cateAndUrlList) {
            //判断是否是第一梯队，或者说是树的根节点，如果是根节点就加入到父类集合与返回集合
            if (disease.getParentId() == 0) {
                parentsList.add(disease);
                resultList.add(disease);
            } else {
                //对于不是第一梯队的数据进行遍历
                for (CateAndUrl parent : parentsList) {
                    //对数据的pid与父类集合中的父节点进行配对，如果配对成功，就把数据加入到父节点中的子节点集合

                    if (parent.getFloder().equals("true") ) {
                        if( parent.getCateId() == disease.getParentId())
                        {
                            parent.getChildUC().add(disease);
                            //当前数据有可能是别的数据的父节点，加到父类容器
                            parentsList.add(disease);
                            break;
                        }

                    }
                    if (parent.getFloder().equals("false") ) {
                        if(parent.getUrlId() == disease.getParentId())
                        {
                            parent.getChildUC().add(disease);
                            //当前数据有可能是别的数据的父节点，加到父类容器
                            parentsList.add(disease);
                            break;
                        }

                    }
                }
            }
        }

        ObjectMapper mapper = new ObjectMapper();
        String Json;
        HashMap<String, Object> result = new HashMap<>();
        // java对象转换为json字符换
        try {
            Json =  mapper.writeValueAsString(resultList);
            result.put("code", 200);
            result.put("msg", "处理成功");
            result.put("data", resultList);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return  JSONObject.toJSONString(result);
    }




    public String setResult(Integer code, String msg) {
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", code);
        result.put("msg", msg);
        String jsonString = JSONObject.toJSONString(result);
        return jsonString;
    }

    //详情页面
    @RequestMapping("/clickUrl")
    public String clickUrl(String urlId)
    {

        UrlList urlList = urlListService.selectUrlListByUrlId(Long.valueOf(urlId));
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data",urlList);
        String jsonString = JSONObject.toJSONString(result);
        return jsonString;
    }


    public String countCateContainUrlNumber(String email){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "查询成功");
        result.put("data",urlCateListService.countCateContainUrlNumber(email));
        String jsonString = JSONObject.toJSONString(result);
        return jsonString;
    }
    //处理成JSON
//    @RequestMapping("/disposeJsonToExcel")
//    public String disposeJsonToExcel(String userId)
//    {
//
//        HashMap<String, Object> result = new HashMap<>();
//        result.put("code", 200);
//        result.put("msg", "查询成功");
//        result.put("data",urlCateListService.getUrl(userId));
//        String jsonString = JSONObject.toJSONString(result);
//        return jsonString;
//    }

    /**
     * json 转 excel
     * @param jsonArray
     * @return
     * @throws IOException
     */
    public HSSFWorkbook jsonToExcel(JSONArray jsonArray) throws IOException {
        Set<String> keys = new HashSet<>();
        // 创建HSSFWorkbook对象
        HSSFWorkbook wb = new HSSFWorkbook();
        // 创建HSSFSheet对象
        HSSFSheet sheet = wb.createSheet("sheet0");


        List<JSONObject> jsonObjects = jsonArray.toList(JSONObject.class);

        // 创建HSSFRow对象
        HSSFRow row ;
        for (int i=0;i<jsonObjects.size();i++){
            String temp = jsonObjects.get(i).keySet().toString();
            String target = temp.substring(1,temp.length()-1);
            keys.add(target);
        }

        for( int i =0 ;i<jsonObjects.size(); i++)
        {
            row = sheet.createRow(i);
            HSSFCell cell = row.createCell(0);
            String temp = jsonObjects.get(i).keySet().toString();
            String target = temp.substring(1,temp.length()-1);
            String target1 = temp.substring(2,temp.length()-2);
            cell.setCellValue(target1);
            cell = row.createCell(1);
            cell.setCellValue(jsonObjects.get(i).get(target).toString().substring(2,temp.length()-2));
        }

        return wb;
    }
    @RequestMapping ("/dataDownload/excel")
    @ResponseBody
    public String exportExcel(String userId, HttpServletResponse response) {
        try {
            //此处为你的json数组
            HSSFWorkbook sheets = jsonToExcel(urlCateListService.getUrl(userId));
            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename=data.xls");
            OutputStream os = response.getOutputStream();
            sheets.write(os);
            sheets.close();
            os.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            HashMap<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("msg", "下载失败");

            return JSON.toJSONString(result);
        }
    }
    @RequestMapping ("/disposeUrl")
    public String disposeUrl(String url){
        HashMap<String, Object> result = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();

        Connection conn = Jsoup.connect(url);
        Document document = null;
        try {
            document = conn.get();
        } catch (IOException e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "url不可用");
            return JSONObject.toJSONString(result);
            // handle error
        }
        result.put("code", 200);
        result.put("msg", "解析成功");
        //添加标题
        data.put("title",document.title());

        Elements linksContent = document.select("meta[name='keywords']");
        Elements linksDescription = document.select("meta[name='description']");
        Elements linksIcon = document.select("link[rel$=icon]");


        String fengli="content=\"(.*?)\"";
        Pattern pafengli=Pattern.compile(fengli);
        Matcher description = pafengli.matcher(linksDescription.toString());
        String target;
        if(description.find())
        {
            target = description.group(1);  //group为捕获组
            data.put("description",target);
        }
        else
        {
            data.put("description","未找到");
        }

        description = pafengli.matcher(linksContent.toString());
        if(description.find())
        {
            target = description.group(1);  //group为捕获组
            data.put("keywords",target);
        }
        else
        {
            data.put("keywords","未找到");
        }
        String fengli1="href=\"(.*?)\"";
        Pattern pafengli1=Pattern.compile(fengli1);
        description = pafengli1.matcher(linksIcon.toString());
        if(description.find())
        {
            target = description.group(1);  //group为捕获组
            try {
                new URL(target).toURI();
                data.put("iconUrl",target);
            } catch (MalformedURLException e) {
                data.put("iconUrl","url不合法");
            } catch (URISyntaxException e) {
                data.put("iconUrl","url不合法");
            }
        }
        else
        {
            data.put("iconUrl","未找到");
        }
        result.put("data",data);
        return JSONObject.toJSONString(result);
    }

    @RequestMapping("/disposeBookmarkExhibitedToJsonNew")
    public String  disposeBookmarkExhibitedToJsonNew(){
        List<UrlList> urlLists = urlListService.selectUrListByNeedLogin();
        List<CateAndUrl> cateAndUrlList = new ArrayList<>();
        Set<String> parentSet = new HashSet<>();
        for( int i = 0 ; i < urlLists.size() ; i++)
        {
            String parentString = urlCateListService.selectUrListCateByUrlCateId(urlLists.get(i).getCateId());
            String [] parentList = parentString.split(",");
            System.out.println(parentString);
            for(String parent : parentList)
            {
                if(parent.equals("0")){
                    continue;
                }
                parentSet.add(parent);
            }
        }
        for (String str : parentSet) {
            UrlCateList urlCateList =urlCateListService.selectByPrimaryKey(Integer.valueOf(str));
            CateAndUrl cateAndUrl = new CateAndUrl();
            cateAndUrl.setFloder("true");
            cateAndUrl.setName(urlCateList.getName());
            cateAndUrl.setParentId(urlCateList.getRootCateId());
            cateAndUrl.setCateId(urlCateList.getId());
            cateAndUrlList.add(cateAndUrl);
        }
        for( int i = 0 ; i < urlLists.size() ; i++){
            UrlList urlList = urlLists.get(i);
            if(urlList.getStatus() == 0 ) continue ;
            CateAndUrl cateAndUrl = new CateAndUrl();
            cateAndUrl.setFloder("false");
            cateAndUrl.setName(urlList.getName());
            cateAndUrl.setParentId(urlList.getCateId());
            cateAndUrl.setUrlId(urlList.getId());
            cateAndUrl.setUrl(urlList.getUrl());
            cateAndUrl.setIcon(urlList.getIcon());
            cateAndUrl.setCreatetime(urlList.getCreatetime());
            cateAndUrl.setViews(urlList.getViews());
            cateAndUrl.setTags(urlList.getTags());
            cateAndUrl.setWeigh(urlList.getWeigh());
            cateAndUrl.setStatus(urlList.getStatus());
            cateAndUrl.setDesc(urlList.getDesc());
            cateAndUrl.setNeedLogin(urlList.getNeedLogin());
            cateAndUrl.setAgentHint(urlList.getAgentHint());
            cateAndUrl.setIsNeedAgent(urlList.getIsNeedAgent());
            cateAndUrl.setIsAd(urlList.getIsAd());
            cateAndUrl.setIsTop(urlList.getIsTop());
            cateAndUrl.setIsEncrypt(urlList.getIsEncrypt());
            for(CateAndUrl cateAndUrl1 : cateAndUrlList){
                if(cateAndUrl.getParentId() == cateAndUrl1.getCateId())
                {
                    cateAndUrl1.getChildUC().add(cateAndUrl);
                    break;
                }
            }
        }
        return JSONObject.toJSONString(cateAndUrlList);


    }


    @RequestMapping("/returnCateByUserId")
    public String returnCateByUserId(String userId)
    {
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("msg", "返回成功");
        List<UrlCateList> urlCateLists = urlCateListService.selectUrListByUserId(Integer.valueOf(userId));
        result.put("data", urlCateLists);
        return JSONObject.toJSONString(result);
    }


    @RequestMapping("/disposeBookmarkToJsonByUserId")
    public String  disposeBookmarkToJsonByUserId(String userId)
    {

        List<UrlCateList> urlCateListList =  urlCateListService.selectUrListByUserId(Integer.valueOf(userId));
        List<CateAndUrl> cateAndUrlList = new ArrayList<>();
        for( int i = 0 ; i < urlCateListList.size() ; i++)
        {
            UrlCateList urlCateList = urlCateListList.get(i);
            CateAndUrl cateAndUrl = new CateAndUrl();
            cateAndUrl.setFloder("true");
            cateAndUrl.setName(urlCateList.getName());
            cateAndUrl.setParentId(urlCateList.getRootCateId());
            cateAndUrl.setCateId(urlCateList.getId());
            cateAndUrlList.add(cateAndUrl);
        }

        System.out.println(cateAndUrlList);
        List<CateAndUrl> parentsList = new ArrayList<>();
        //声明返回集合
        List<CateAndUrl> resultList = new ArrayList<>();
        //对全部数据进行遍历
        for (CateAndUrl disease : cateAndUrlList) {
            //判断是否是第一梯队，或者说是树的根节点，如果是根节点就加入到父类集合与返回集合
            if (disease.getParentId() == 0) {
                parentsList.add(disease);
                resultList.add(disease);
            } else {
                //对于不是第一梯队的数据进行遍历
                for (CateAndUrl parent : parentsList) {
                    //对数据的pid与父类集合中的父节点进行配对，如果配对成功，就把数据加入到父节点中的子节点集合

                    if (parent.getFloder().equals("true") ) {
                        if( parent.getCateId() == disease.getParentId())
                        {
                            parent.getChildUC().add(disease);
                            //当前数据有可能是别的数据的父节点，加到父类容器
                            parentsList.add(disease);
                            break;
                        }

                    }
                    if (parent.getFloder().equals("false") ) {
                        if(parent.getUrlId() == disease.getParentId())
                        {
                            parent.getChildUC().add(disease);
                            //当前数据有可能是别的数据的父节点，加到父类容器
                            parentsList.add(disease);
                            break;
                        }

                    }
                }
            }
        }

        ObjectMapper mapper = new ObjectMapper();
        String Json;
        HashMap<String, Object> result = new HashMap<>();
        // java对象转换为json字符换
        try {
            Json =  mapper.writeValueAsString(resultList);
            result.put("code", 200);
            result.put("msg", "处理成功");
            result.put("data", resultList);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return  JSONObject.toJSONString(result);
    }

    @RequestMapping("/insertCateByUser")
    public String  insertCateByUser(UrlCateList urlCateList)
    {
        int flag = 0;
        Date date = new Date();
        urlCateList.setCreatetime(date.getTime());
        urlCateList.setUrlNumber(Long.valueOf(0));
        urlCateList.setRootCateId(0);
        HashMap<String, Object> result = new HashMap<>();
        flag = urlCateListService.insertSelective(urlCateList);
        if(flag == 1)
        {
            result.put("code", 200);
            result.put("msg", "处理成功");
        }else{
            result.put("code", 500);
            result.put("msg", "处理失败");
        }
        return JSONObject.toJSONString(result);
    }

    @RequestMapping("/deleteCateByCateId")
    public String  deleteCateByCateId(Integer Id,String userId)
    {
        HashMap<String, Object> result = new HashMap<>();
        Set<Integer> parentList = new HashSet<>();
        parentList.add(Integer.valueOf(Id));

        List<String> stringList = urlCateListService.selectChildrenByCateId(Id);

        String childrenString= String.join(",", stringList);
        String [] strings = childrenString.split(",");
        Set<String> set = new HashSet<>(Arrays.asList(strings));
        for(String child : set){
            if(child.equals("")) {
                urlListService.deleteByCateId(Long.valueOf(Id));
                urlCateListService.deleteByPrimaryKey(Id);
            }else{
                urlListService.deleteByCateId(Long.valueOf(child));
                urlCateListService.deleteByPrimaryKey(Integer.valueOf(child));
            }

        }

        urlCateListService.countCateContainUrlNumber(userDao.selectByPrimaryKey(Integer.valueOf(userId)).getUserEmail());
            result.put("code", 200);
            result.put("msg", "处理成功");
        return JSONObject.toJSONString(result);
    }

}
