import configparser

class LocatorManager:
    def __init__(self, config_file='config.ini'):
        self.config_file = config_file
        self.selectors = {}
        self.config = configparser.ConfigParser()
        self.load_locators_from_config()

    def load_locators_from_config(self):
        """
        config.ini 파일에서 선택자를 불러와 self.selectors에 저장
        """

        with open(self.config_file, 'r', encoding='utf-8') as config_file:
            self.config.read_file(config_file)

        # PriceLocators 섹션
        self.selectors['PriceLocators'] = {
            'return_fee_input_locator': self.config.get('PriceLocators', 'return_fee_input_locator').strip("'"),
            'first_delv_fee_input_locator': self.config.get('PriceLocators', 'first_delv_fee_input_locator').strip("'"),
            'exchange_fee_input_locator': self.config.get('PriceLocators', 'exchange_fee_input_locator').strip("'"),
            'plus_margin_locator': self.config.get('PriceLocators', 'plus_margin_locator').strip("'"),
            'oversea_shipping_locator': self.config.get('PriceLocators', 'oversea_shipping_locator').strip("'"),
            'option_count_text_locator': self.config.get('PriceLocators', 'option_count_text_locator').strip("'"),
            'product_cost_locator': self.config.get('PriceLocators', 'product_cost_locator').strip("'"),
            'standard_selling_price_locator': self.config.get('PriceLocators', 'standard_selling_price_locator').strip("'"),
        }

        # BrowserControl 섹션
        self.selectors['BrowserControl'] = {
            'login_email_locator': self.config.get('BrowserControl', 'login_email_locator').strip("'"),
            'login_password_locator': self.config.get('BrowserControl', 'login_password_locator').strip("'"),
            'login_button_locator': self.config.get('BrowserControl', 'login_button_locator').strip("'"),
            'admin_toggle_locator': self.config.get('BrowserControl', 'admin_toggle_locator').strip("'"),
            'staff_id_locator': self.config.get('BrowserControl', 'staff_id_locator').strip("'"),
            'staff_login_button_locator': self.config.get('BrowserControl', 'staff_login_button_locator').strip("'"),
            'close_ad_dialog_locator': self.config.get('BrowserControl', 'close_ad_dialog_locator').strip("'"),
            'close_ad_button_locator': self.config.get('BrowserControl', 'close_ad_button_locator').strip("'"),
            'total_product_count_locator': self.config.get('BrowserControl', 'total_product_count_locator').strip("'"),
            'product_name_template': self.config.get('BrowserControl', 'product_name_template').strip("'"),
            'product_price_template': self.config.get('BrowserControl', 'product_price_template').strip("'"),
            'product_image_template': self.config.get('BrowserControl', 'product_image_template').strip("'"),
            'new_product_page_locator': self.config.get('BrowserControl', 'new_product_page_locator').strip("'"),
            'current_page_locator': self.config.get('BrowserControl', 'current_page_locator').strip("'"),
            'next_page_button_template': self.config.get('BrowserControl', 'next_page_button_template').strip("'"),
            'source_button_locator': self.config.get('BrowserControl', 'source_button_locator').strip("'"),
            'ck_source_editing_area_locator': self.config.get('BrowserControl', 'ck_source_editing_area_locator').strip("'"),
            'title_tab_locator': self.config.get('BrowserControl', 'title_tab_locator').strip("'"),
            'option_tab_locator': self.config.get('BrowserControl', 'option_tab_locator').strip("'"),
            'price_tab_locator': self.config.get('BrowserControl', 'price_tab_locator').strip("'"),
            'tag_tab_locator': self.config.get('BrowserControl', 'tag_tab_locator').strip("'"),
            'thumb_tab_locator': self.config.get('BrowserControl', 'thumb_tab_locator').strip("'"),
            'detail_tab_locator': self.config.get('BrowserControl', 'detail_tab_locator').strip("'"),
            'upload_tab_locator': self.config.get('BrowserControl', 'upload_tab_locator').strip("'"),
            'save_button_locator': self.config.get('BrowserControl', 'save_button_locator').strip("'"),
        }

        # OptionLocators 섹션
        self.selectors['OptionLocators'] = {
            'option_excluded_selector_template': self.config.get('OptionLocators', 'option_excluded_selector_template').strip("'"),
            'option_input_selector_template': self.config.get('OptionLocators', 'option_input_selector_template').strip("'"),
            'single_option_locator': self.config.get('OptionLocators', 'single_option_locator').strip("'"),
            'option_product_locator': self.config.get('OptionLocators', 'option_product_locator').strip("'"),
            'total_options_selector': self.config.get('OptionLocators', 'total_options_selector').strip("'"),
            'original_name_selector_template': self.config.get('OptionLocators', 'original_name_selector_template').strip("'"),
            'edit_field_selector_template': self.config.get('OptionLocators', 'edit_field_selector_template').strip("'"),
            'checkbox_selector_template': self.config.get('OptionLocators', 'checkbox_selector_template').strip("'"),
            'image_selector_template': self.config.get('OptionLocators', 'image_selector_template').strip("'"),
            'price_selector_template': self.config.get('OptionLocators', 'price_selector_template').strip("'"),
            'delete_button_selector_template': self.config.get('OptionLocators', 'delete_button_selector_template').strip("'"),
            'confirm_delete_button_locator': self.config.get('OptionLocators', 'confirm_delete_button_locator').strip("'"),
            'add_button_selector_template': self.config.get('OptionLocators', 'add_button_selector_template').strip("'"),
            'file_input_locator': self.config.get('OptionLocators', 'file_input_locator').strip("'"),
        }

        # TitleLocators 섹션
        self.selectors['TitleLocators'] = {
            'product_name_input_locator': self.config.get('TitleLocators', 'product_name_input_locator').strip("'"),
            'product_name_suggestion_input_locator': self.config.get('TitleLocators', 'product_name_suggestion_input_locator').strip("'"),
            'product_name_search_button_locator': self.config.get('TitleLocators', 'product_name_search_button_locator').strip("'"),
            'original_product_name_locator': self.config.get('TitleLocators', 'original_product_name_locator').strip("'"),
            'product_name_warning_delete_button_locator': self.config.get('TitleLocators', 'product_name_warning_delete_button_locator').strip("'"),
            'category_suggestion_button_locator': self.config.get('TitleLocators', 'category_suggestion_button_locator').strip("'"),
            'category_main_selector_with_cp': self.config.get('TitleLocators', 'category_main_selector_with_cp').strip("'"),
            'category_main_selector_with_ss': self.config.get('TitleLocators', 'category_main_selector_with_ss').strip("'"),
            'category_main_selector_with_esm': self.config.get('TitleLocators', 'category_main_selector_with_esm').strip("'"),
            'category_text_locator': self.config.get('TitleLocators', 'category_text_locator').strip("'"),
            'category_text_locator_certified': self.config.get('TitleLocators', 'category_text_locator_certified').strip("'"),
        }


    def get_locator(self, section, key):
        """
        섹션과 키를 받아서 해당 선택자를 반환
        """
        return self.selectors.get(section, {}).get(key, None)

    def get_category_data(self, section, category):
        """
        Config.ini에서 특정 카테고리에 대한 데이터를 반환하는 메서드
        """
        try:
            category_data = self.config[section][category]
            return category_data
        except KeyError:
            self.logger.error(f"{section} 섹션에서 {category} 카테고리를 찾을 수 없습니다.")
            return None
