import csv

class JusoDB:
    def __init__(self, path_csv):
        self.path = path_csv
        self.dict_juso = []

        self.ReadCSV(path_csv)

    def ReadCSV(self, path):
        # CSV 파일 읽기
        with open(path, mode='r', newline='', encoding='utf-8') as file:
            reader = csv.DictReader(file)
            # 헤더 읽기
            header = next(reader) 

            # 각 행을 딕셔너리로 저장
            for row in reader:
                self.dict_juso.append(row)

        print(f"Juso Count : {len(self.dict_juso)}")

    def CompareAndFindValue(self, src, trg, value):
        retValue = ""
        for row in self.dict_juso:
            temp1 = value.replace(" ", "").strip()
            temp2 = row[src].replace(" ","").strip()
            if temp1 == temp2:
                retValue = row[trg]
                break;
        
        return retValue

    def ConvJusoToRoad(self, juso):
        return self.CompareAndFindValue("jibun", "road", juso)
    
    def ConvRoadToJibun(self, juso):
        return self.CompareAndFindValue("road", "jibun", juso)
    
    def GetRoadArea(self, road):
        if road == "":
            return ""
        
        #서울시 용산구 무시무시로20길 99-99 <- 현재까지 가진 데이터는 전부 이런 형식  
        words = road.split()
        if len(words) < 3:
            return ""
        
        return words[2]

    