package com.caliverse.admin.domain.datacomponent;


import com.caliverse.admin.domain.entity.EMetaData;
import com.caliverse.admin.domain.entity.metadata.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Getter
@Slf4j
public class MetaDataFileLoader_old {
    @Value("${caliverse.metadata.path}")
    private String metaDataPath;

//    private Map<Integer, MetaItemData> items;
//    private Map<String, MetaTextStringData> textStrings;
//    private Map<Integer, MetaClothTypeData> clothTypes;
//    private Map<Integer, MetaToolData> toolItems;
//    private Map<Integer, String> banWords;
//    private Map<MetaQuestKey, MetaQuestData> quests;
//    private Map<Integer, MetaBuildingData> buildings;
//    private Map<Integer, MetaLandData> lands;
//
//    public MetaTextStringData getName(String text){
//        if(textStrings == null) parseMetaString();
//        if(text == null) return null;
//
//        MetaTextStringData name = textStrings.get(text);
//
//        return name;
//    }
//
//    //아이템 가져오기
//    public MetaItemData getMetaItem(int itemId) {
//        if(textStrings == null) parseMetaString();
//        if(items == null){
//            parseMetaItems();
//        }
//        if(itemId == 0) return null;
//
//        MetaItemData itemData = items.get(itemId);
//
//        return itemData;
//    }
//
//    //의상 타입 가져오기
//    public MetaClothTypeData getMetaClothType(int id) {
//        if(clothTypes == null ){
//            parseMetaClothTypes();
//        }
//        if(id == 0) return null;
//
//        return clothTypes.get(id);
//    }
//
//    //도구 정보 가져오기
//    public MetaToolData getMetaToolItem(int id) {
//        if(textStrings == null) parseMetaString();
//        if(toolItems == null ){
//            parseMetaToolItems();
//        }
//        if(id == 0){
//            return null;
//        }
//
//        return toolItems.get(id);
//    }
//
//    //퀘스트 정보 가져오기
//    public List<MetaQuestData> getMetaQuests(int id) {
//        if(textStrings == null) parseMetaString();
//        if(quests == null ) parseMetaQuests();
//        if(id == 0) return null;
//
//        List<MetaQuestData> quest = new ArrayList<>();
//        quests.forEach((key,val)->{
//            if(key.getQeustId().equals(id)) quest.add(val);
//        });
//
//        return quest;
//    }
//
//    //빌딩 정보 가져오기
//    public MetaBuildingData getMetaBuildingItem(int id) {
//        if(textStrings == null) parseMetaString();
//        if(buildings == null ){
//            parseMetaBuilding();
//        }
//        if(id == 0){
//            return null;
//        }
//
//        return buildings.get(id);
//    }
//
//    //랜드 정보 가져오기
//    public MetaLandData getMetaLandItem(int id) {
//        if(textStrings == null) parseMetaString();
//        if(lands == null ){
//            parseMetaLand();
//        }
//        if(id == 0){
//            return null;
//        }
//
//        return lands.get(id);
//    }
//
//    // 아이템 정보 데이터 저장
//    public void parseMetaItems(){
//        items = new HashMap<>();
//
//        List<Map<String, Object>> itemInfos = loadDataFromJsonFile(EMetaData.ITEM_DATA.getFileName(), "ItemMetaDataList");
//
//        for (Map<String, Object> itemInfo : itemInfos) {
//            MetaItemData item = new MetaItemData();
//            item.setItemId((Integer)itemInfo.get("item_id"));
//            item.setName((String)itemInfo.get("name"));
//            item.setLargeType((String)itemInfo.get("type_large"));
//            item.setGender((String)itemInfo.get("gender"));
//            items.put(item.getItemId(), item);
//        }
//
//        log.info("parseMetaItems ItemMetaDataList.json Load Complete");
//
//    }
//
//    // 의상 타입 데이터 저장
//    public void parseMetaClothTypes(){
//        clothTypes = new HashMap<>();
//
//        List<Map<String, Object>> itemInfos = loadDataFromJsonFile(EMetaData.CLOTH_TYPE_DATA.getFileName(), "ClothEquipTypeDataList");
//
//        for (Map<String, Object> itemInfo : itemInfos) {
//            MetaClothTypeData item = new MetaClothTypeData();
//            item.setMetaId((Integer)itemInfo.get("Meta_id"));
//            item.setSmallType((String)itemInfo.get("SmallType"));
//            item.setEquipSlotType((String)itemInfo.get("EquipSlotType"));
//            clothTypes.put(item.getMetaId(), item);
//        }
//        log.info("parseMetaClothTypes ClothEquipTypeDataList.json Load Complete");
//    }
//
//    // 도구 정보 데이터 저장
//    public void parseMetaToolItems(){
//        toolItems = new HashMap<>();
//
//        List<Map<String, Object>> itemInfos = loadDataFromJsonFile(EMetaData.TOOL_DATA.getFileName(), "ToolMetaDataList");
//
//        for (Map<String, Object> itemInfo : itemInfos) {
//            MetaToolData item = new MetaToolData();
//            item.setToolId((Integer)itemInfo.get("tool_id"));
//            item.setToolName((String)itemInfo.get("tool_name"));
//            toolItems.put(item.getToolId(), item);
//        }
//        log.info("parseMetaToolItems ToolMetaDataList.json Load Complete");
//    }
//
//    // 금지어 데이터 저장
//    public void parseMetaBanWord(){
//        banWords = new HashMap<>();
//
//        List<Map<String, Object>> itemInfos = loadDataFromJsonFile(EMetaData.BAN_WORD_DATA.getFileName(), "BanWordMetaDataList");
//
//        for (Map<String, Object> itemInfo : itemInfos) {
//            banWords.put((Integer)itemInfo.get("Id"), (String)itemInfo.get("Ban_Word"));
//        }
//        log.info("parseMetaBanWord BanWordMetaDataList.json Load Complete");
//    }
//
//    // String 데이터 저장
//    public void parseMetaString(){
//        textStrings = new HashMap<>();
//
//        List<Map<String, Object>> itemInfos = loadDataFromJsonFile(EMetaData.TEXT_STRING_DATA.getFileName(), "TextStringMetaDataList");
//
//        for (Map<String, Object> itemInfo : itemInfos) {
//            MetaTextStringData item = new MetaTextStringData();
//            item.setKey((String)itemInfo.get("Key"));
//            item.setKor((String)itemInfo.get("SourceString"));
//            item.setEn((String)itemInfo.get("en"));
//            textStrings.put((String)itemInfo.get("Key"), item);
//        }
//        log.info("parseMetaString TextStringMetaDataList.json Load Complete");
//    }
//
//    // 퀘스트 데이터 저장
//    public void parseMetaQuests(){
//        quests = new HashMap<>();
//
//        List<Map<String, Object>> itemInfos = loadDataFromJsonFile(EMetaData.QUEST_DATA.getFileName(), "QuestMetaDataList");
//
//        for (Map<String, Object> itemInfo : itemInfos) {
//            MetaQuestData item = new MetaQuestData();
//            Integer questId = (Integer)itemInfo.get("QuestID");
//            Integer taskNum = (Integer)itemInfo.get("TaskNum");
//            item.setQuestId(questId);
//            item.setTaskNum(taskNum);
//            item.setTaskName((String)itemInfo.get("TaskName"));
//            item.setCounter((Integer) itemInfo.get("SetCounter"));
//            quests.put(new MetaQuestKey(questId, taskNum), item);
//        }
//        log.info("parseMetaQuests QuestMetaDataList.json Load Complete");
//    }
//
//    // 빌딩 데이터 저장
//    public void parseMetaBuilding(){
//        buildings = new HashMap<>();
//
//        List<Map<String, Object>> buildingInfos = loadDataFromJsonFile(EMetaData.BUILDING_DATA.getFileName(), "BuildingMetaDataList");
//
//        for (Map<String, Object> itemInfo : buildingInfos) {
//            MetaBuildingData item = new MetaBuildingData();
//            item.setBuildingId((Integer)itemInfo.get("BuildingId"));
//            item.setBuildingOpen((Boolean)itemInfo.get("BuildingOpen"));
//            item.setOwner((String)itemInfo.get("Owner"));
//            item.setBuildingName((String)itemInfo.get("BuildingName"));
//            item.setBuildingDesc((String)itemInfo.get("BuildingDesc"));
//            item.setBuildingSize((String)itemInfo.get("BuildingSize"));
//            item.setInstanceSocket((Integer)itemInfo.get("InstanceSocket"));
//            item.setInstanceSocketLink((Integer)itemInfo.get("InstanceSocketLink"));
//            buildings.put((Integer)itemInfo.get("BuildingId"), item);
//        }
//        log.info("parseMetaBuilding BuildingMetaDataList.json Load Complete");
//    }
//
//    // 랜드 데이터 저장
//    public void parseMetaLand(){
//        lands = new HashMap<>();
//
//        List<Map<String, Object>> landInfos = loadDataFromJsonFile(EMetaData.LAND_DATA.getFileName(), "LandMetaDataList");
//
//        for (Map<String, Object> itemInfo : landInfos) {
//            MetaLandData item = new MetaLandData();
//            item.setLandId((Integer)itemInfo.get("LandId"));
//            item.setOwner((String)itemInfo.get("Owner"));
//            item.setLandName((String)itemInfo.get("LandName"));
//            item.setLandDesc((String)itemInfo.get("LandDesc"));
//            item.setLandSize((String)itemInfo.get("LandSize"));
//            item.setLandType((String)itemInfo.get("LandType"));
//            item.setBuildingSocket((Integer)itemInfo.get("BuildingSocket"));
//            lands.put((Integer)itemInfo.get("LandId"), item);
//        }
//        log.info("parseMetaLand LandMetaDataList.json Load Complete");
//    }
//
//    // resource json 파일 정보 읽어오기(resource)
////    public List<Map<String, Object>> loadDataFromJsonFile(String fileName, String listName) {
////        List<Map<String, Object>> resultMap = new ArrayList<>();
////
////        URL resourceUrl = getClass().getClassLoader().getResource("metadata/" + fileName);
////        log.info("loadDataFromJsonFile path: {}", resourceUrl.getPath());
////
////        try (InputStream inputStream = resourceUrl.openStream()) {
////            if (inputStream != null) {
////                boolean isRequired = EMetaData.getIsRequired(fileName);
////                if (isRequired) {
////                    String str = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
////                    ObjectMapper objectMapper = new ObjectMapper();
////                    try {
////                        JsonNode rootNode = objectMapper.readTree(str);
////
////                        resultMap = objectMapper.readValue(rootNode.get(listName).toString(), new TypeReference<List<Map<String, Object>>>() {});
////                    } catch (JsonMappingException e) {
////                        log.error("loadDataFromJsonFile JsonMappingException: {}", e.getMessage());
////                    } catch (JsonProcessingException e) {
////                        log.error("loadDataFromJsonFile JsonProcessingException: {}", e.getMessage());
////                    }
////                }
////            } else {
////                log.error("Resource not found: {}", fileName);
////            }
////        } catch (IOException e) {
////            log.error("loadDataFromJsonFile IOException: {}", e.getMessage());
////        }
////
////        return resultMap;
////    }
//    // json 파일 정보 읽어오기
//    public List<Map<String, Object>> loadDataFromJsonFile(String fileName, String listName) {
//        List<Map<String, Object>> resultMap = new ArrayList<>();
//
//        File file = new File(metaDataPath, fileName);
//        log.info("loadDataFromJsonFile path: {}", file.getPath());
//        if (file.isFile()) {
//            boolean isRequired = EMetaData.getIsRequired(fileName);
//            if (isRequired) {
//                String str = readFileAsString(file);
//                ObjectMapper objectMapper = new ObjectMapper();
//                try {
//                    JsonNode rootNode = objectMapper.readTree(str);
//
//                    resultMap = objectMapper.readValue(rootNode.get(listName).toString(), new TypeReference<List<Map<String, Object>>>() {});
//                } catch (JsonMappingException e) {
//                    log.error("loadDataFromJsonFile JsonMappingException: {}", e.getMessage());
//                } catch (JsonProcessingException e) {
//                    log.error("loadDataFromJsonFile JsonProcessingException: {}", e.getMessage());
//                }
//                finally {
//                    file = null;
//                }
//            }
//        }
//        return resultMap;
//    }
////    public List<Map<String, Object>> loadDataFromJsonFile(String fileName, String listName) {
////        List<Map<String, Object>> resultMap = new ArrayList<>();
////        File file = new File(getClass().getClassLoader().getResource("metadata").getPath(), fileName);
//////        File file = new File(metaDataPath, fileName);
////        if (file.isFile()) {
////            boolean isRequired = EMetaData.getIsRequired(fileName);
////            if (isRequired) {
////
////                String str = readFileAsString(file);
////                ObjectMapper objectMapper = new ObjectMapper();
////                try {
////                    // Parse the JSON object
////                    JsonNode rootNode = objectMapper.readTree(str);
////
//////                    jsonMap = objectMapper.readValue(str, new TypeReference<Map<String, Object>>() {});
////                    resultMap = objectMapper.readValue(rootNode.get(listName).toString(), new TypeReference<List<Map<String, Object>>>() {});
////                } catch (JsonMappingException e) {
////                    e.printStackTrace();
////                } catch (JsonProcessingException e) {
////                    e.printStackTrace();
////                }
////                finally {
////                    file = null;
////                }
////            }
////        }
////        return resultMap;
////    }
//
//    private String readFileAsString(File file) {
//        StringBuilder fileContents = new StringBuilder((int)file.length());
//        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
//            String line;
//            while((line = br.readLine()) != null) {
//                fileContents.append(line);
//            }
//        } catch (IOException e) {
//            log.error("readFileAsString IOException: {}", e.getMessage());
//        }
//        return fileContents.toString();
//    }
}
