from merge_class.merge import merge
from Load_process.LoadData import Load_Data_Prepare, Load_Data_Tools
from model_data_processing.processing import make_label_list
from utils.Stomach_Config import Loading_Config

class Load_Indepentend_Data():
    def __init__(self, OneHot_Encording):
        '''
        影像切割物件
        label有2類,會將其轉成one-hot-encoding的形式
            [0, 1] = NPC_negative
            [1, 0] = NPC_positive
        '''
        self.merge = merge()
        self.OneHot_Encording = OneHot_Encording
        pass

    def process_main(self, Test_data_root, Test_mask_root):
        self.test, self.test_label, self.test_mask = self.get_Independent_image(Test_data_root, Test_mask_root)
        print("\ntest_labels有" + str(len(self.test_label)) + "筆資料\n")

    def get_Independent_image(self, independent_DataRoot, independent_MaskRoot):
        Total_Size_List = []
        Total_Dict_Data_Root = self.Get_Independent_data_Root(independent_DataRoot, Loading_Config["Training_Labels"], len(Loading_Config["Training_Labels"])) # 讀取測試資料集的資料
        Total_Dict_Mask_Root = self.Get_Independent_data_Root(independent_MaskRoot, Loading_Config["XML_Loading_Label"], len(Loading_Config["XML_Loading_Label"])) # 讀取測試資料集的mask資料

        # 將測試資料字典轉成列表，並且將其排序
        Total_List_Data_Root = []
        for Label in Loading_Config["Training_Labels"]:
            Total_List_Data_Root.append(Total_Dict_Data_Root[Label])

        # 將測試資料字典轉成列表，並且將其排序
        Total_List_Mask_Data_Root = []
        for Label in Loading_Config["XML_Loading_Label"]:
            Total_List_Mask_Data_Root.append(Total_Dict_Mask_Root[Label])
        
        classify_image, Classify_Label = [], []
        i = 0 # 計算classify_image的counter，且計算總共有幾筆資料
        for test_title in Total_List_Data_Root: # 藉由讀取所有路徑來進行讀檔
            test_label = make_label_list(len(test_title), self.OneHot_Encording[i]) # 製作對應圖片數量的label出來+    
            print(Loading_Config["Training_Labels"][i] + " 有 " + str(len(test_label)) + " 筆資料 ")

            Total_Size_List.append(len(test_label))

            classify_image.append(test_title)
            Classify_Label.append(test_label)
            i += 1

        classify_Mask_image = []
        i = 0 # 計算classify_image的counter，且計算總共有幾筆資料
        for test_title in Total_List_Mask_Data_Root: # 藉由讀取所有路徑來進行讀檔
            print(Loading_Config["XML_Loading_Label"][i] + " 有 " + str(len(test_title)) + " 筆資料 ")

            classify_Mask_image.append(test_title)
            i += 1

        test = self.merge.merge_data_main(classify_image, 0, len(Loading_Config["Training_Labels"]))
        test_label = self.merge.merge_data_main(Classify_Label, 0, len(Loading_Config["Training_Labels"]))
        test_Mask = self.merge.merge_data_main(classify_Mask_image, 0, len(Loading_Config["XML_Loading_Label"]))
        
        return test, test_label, test_Mask
    
    
    def Get_Independent_data_Root(self, load_data_root, Dictory_Keys, Length):
        Prepare = Load_Data_Prepare()
        Load_Tool = Load_Data_Tools()

        Prepare.Set_Data_Content([], Length)
        Prepare.Set_Data_Dictionary(Dictory_Keys, Prepare.Get_Data_Content(), Length)
        Get_Data_Dict_Content = Prepare.Get_Data_Dict()
        Total_Data_Roots = Load_Tool.get_data_root(load_data_root, Get_Data_Dict_Content, Dictory_Keys)

        return Total_Data_Roots
