package com.org.hktshop.service.global.impl;

import com.org.hktshop.base.BaseService;
import com.org.hktshop.dto.HKtLanguageNo;
import com.org.hktshop.dto.LanguageDto;
import com.org.hktshop.entity.global.HktLanguageInfo;
import com.org.hktshop.service.global.HktLanguageInfoService;
import com.org.hktshop.utils.ExecSqlLib;
import com.org.hktshop.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HktLanguageInfoServiceImpl extends BaseService<HktLanguageInfo> implements HktLanguageInfoService {

    @Override
    public Map<String, String> getLanguageInfo(HktLanguageInfo hktLanguageInfo) {
        Map<String, String> result = new HashMap<>();
        List<HktLanguageInfo> hktLanguageInfos = this.queryList(hktLanguageInfo);
        hktLanguageInfos.forEach(hktLanguageInfo1 -> {
            result.put(hktLanguageInfo1.getLanguageNo(), hktLanguageInfo1.getLanguageDec());
        });
        return result;
    }

    @Override
    public void saveLanguageInfo(List<HktLanguageInfo> hktLanguageInfos) {
        String sql = ExecSqlLib.GetQuerySql(new HktLanguageInfo()).replace(" order by id", " order by language_no desc limit 1");
        HktLanguageInfo languageInfo = this.baseJdbcDao.query(sql, new HktLanguageInfo());
        String no = languageInfo.getLanguageNo().substring(3);
        String languageNo = "No." + (Integer.parseInt(no) + 1);
        for (HktLanguageInfo hktLanguageInfo : hktLanguageInfos) {
            hktLanguageInfo.setLanguageNo(languageNo);
            this.updateJdbc(hktLanguageInfo);
        }
    }

    @Override
    public void updateLanguageInfo(List<HktLanguageInfo> hktLanguageInfos) {
        for (HktLanguageInfo hktLanguageInfo : hktLanguageInfos) {
            this.updateJdbc(hktLanguageInfo);
        }
    }

    @Override
    public void deleteLanguageInfo(String languageNo) {
        HktLanguageInfo hktLanguageInfo=new HktLanguageInfo();
        hktLanguageInfo.setLanguageNo(languageNo);
        this.delete(hktLanguageInfo);
    }

    @Override
    public LanguageDto getLanguages(HktLanguageInfo hktLanguageInfo) {
        LanguageDto res = new LanguageDto();
        String infoSql = "";
        if(StringUtils.isNull(hktLanguageInfo.getLanguageDec())){
            infoSql = ExecSqlLib.GetQuerySql(hktLanguageInfo);
        }else {
            infoSql = ExecSqlLib.GetQuerySql(hktLanguageInfo).replace("language_dec = '"+hktLanguageInfo.getLanguageDec()+"'","language_dec like '%"+hktLanguageInfo.getLanguageDec()+"%'");
        }
        String sql = infoSql.replace(" order by id", " order by language_no");
        List<HktLanguageInfo> languageInfoList = this.baseJdbcDao.queryList(sql, new HktLanguageInfo());
        Set<HktLanguageInfo> listInfo = new HashSet<>();
        for(HktLanguageInfo hktLanguageInfo1 : languageInfoList){
            HktLanguageInfo hktLanguageInfo2 = new HktLanguageInfo();
            hktLanguageInfo2.setLanguageNo(hktLanguageInfo1.getLanguageNo());
            String s = ExecSqlLib.GetQuerySql(hktLanguageInfo2).replace(" order by id", " order by language_no");
            List<HktLanguageInfo> list = this.baseJdbcDao.queryList(s, hktLanguageInfo2);
            listInfo.addAll(list);
        }
        Map<String, List<HktLanguageInfo>> map = listInfo.stream().collect(Collectors.groupingBy(HktLanguageInfo::getLanguageNo));
        List<Map.Entry<String, List<HktLanguageInfo>>> list = new ArrayList<>(map.entrySet());
        list.sort(Comparator.comparing(Map.Entry::getKey));
        List<HKtLanguageNo> languageNoList = new ArrayList<>();
        for (Map.Entry<String, List<HktLanguageInfo>> entry : list) {
            HKtLanguageNo languageNo = new HKtLanguageNo();
            languageNo.setLanguageNo(entry.getKey());
            languageNo.setLanguageInfo(entry.getValue());
            languageNoList.add(languageNo);
        }
        Map<String, List<HktLanguageInfo>> nameMap = listInfo.stream().collect(Collectors.groupingBy(HktLanguageInfo::getLanguageName));
        List<String> languageNames = new ArrayList<>();
        for (Map.Entry<String, List<HktLanguageInfo>> entry : nameMap.entrySet()) {
            languageNames.add(entry.getKey());
        }
        res.setLanguageNames(languageNames);
        res.setLanuageInfos(languageNoList);
        return res;
    }

    @Override
    public Set<String> getPage() {
        String sql = ExecSqlLib.GetQuerySql(new HktLanguageInfo());
        List<HktLanguageInfo> hktLanguageInfos = this.baseJdbcDao.queryList(sql, new HktLanguageInfo());
        Set<String> res = new HashSet<>();
        for (HktLanguageInfo languageInfo : hktLanguageInfos) {
            res.add(languageInfo.getPage());
        }
        return res;
    }

}
