######################## BEGIN LICENSE BLOCK ########################
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#   Mark Pilgrim - port to Python
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA
######################### END LICENSE BLOCK #########################

from .charsetprober import CharSetProber
from .codingstatemachine import CodingStateMachine
from .enums import LanguageFilter, ProbingState, MachineState
from .escsm import (HZ_SM_MODEL, ISO2022CN_SM_MODEL, ISO2022JP_SM_MODEL,
                    ISO2022KR_SM_MODEL)


class EscCharSetProber(CharSetProber):
    """
    This CharSetProber uses a "code scheme" approach for detecting encodings,
    whereby easily recognizable escape or shift sequences are relied on to
    identify these encodings.
    """

    def __init__(self, lang_filter=None):
        # 初始化EscCharSetProber类
        super(EscCharSetProber, self).__init__(lang_filter=lang_filter)
        # 初始化编码状态机列表
        self.coding_sm = []
        # 如果语言过滤器包含简体中文
        if self.lang_filter & LanguageFilter.CHINESE_SIMPLIFIED:
            # 添加简体中文编码状态机
            self.coding_sm.append(CodingStateMachine(HZ_SM_MODEL))
            # 添加ISO2022CN编码状态机
            self.coding_sm.append(CodingStateMachine(ISO2022CN_SM_MODEL))
        # 如果语言过滤器包含日语
        if self.lang_filter & LanguageFilter.JAPANESE:
            # 添加ISO2022JP编码状态机
            self.coding_sm.append(CodingStateMachine(ISO2022JP_SM_MODEL))
        # 如果语言过滤器包含韩语
        if self.lang_filter & LanguageFilter.KOREAN:
            # 添加ISO2022KR编码状态机
            self.coding_sm.append(CodingStateMachine(ISO2022KR_SM_MODEL))
        # 初始化活动状态机数量
        self.active_sm_count = None
        # 初始化检测到的字符集
        self._detected_charset = None
        # 初始化检测到的语言
        self._detected_language = None
        # 初始化状态
        self._state = None
        # 重置
        self.reset()

    def reset(self):
        # 重置EscCharSetProber类
        super(EscCharSetProber, self).reset()
        # 遍历编码状态机列表
        for coding_sm in self.coding_sm:
            # 如果编码状态机为空，则跳过
            if not coding_sm:
                continue
            # 设置编码状态机为活动状态
            coding_sm.active = True
            # 重置编码状态机
            coding_sm.reset()
        # 设置活动状态机数量为编码状态机列表的长度
        self.active_sm_count = len(self.coding_sm)
        # 设置检测到的字符集为空
        self._detected_charset = None
        # 设置检测到的语言为空
        self._detected_language = None

    @property
    def charset_name(self):
        # 返回检测到的字符集
        return self._detected_charset

    @property
    def language(self):
        # 返回检测到的语言
        return self._detected_language

    def get_confidence(self):
        # 如果检测到了字符集，则返回0.99，否则返回0.00
        if self._detected_charset:
            return 0.99
        else:
            return 0.00

    def feed(self, byte_str):
        # 遍历字节字符串
        for c in byte_str:
            # 遍历编码状态机列表
            for coding_sm in self.coding_sm:
                # 如果编码状态机为空或非活动状态，则跳过
                if not coding_sm or not coding_sm.active:
                    continue
                # 获取编码状态机的下一个状态
                coding_state = coding_sm.next_state(c)
                # 如果状态为错误，则设置编码状态机为非活动状态，活动状态机数量减一
                if coding_state == MachineState.ERROR:
                    coding_sm.active = False
                    self.active_sm_count -= 1
                    # 如果活动状态机数量小于等于0，则设置状态为非匹配
                    if self.active_sm_count <= 0:
                        self._state = ProbingState.NOT_ME
                        return self.state
                # 如果状态为匹配，则设置状态为匹配，设置检测到的字符集和语言
                elif coding_state == MachineState.ITS_ME:
                    self._state = ProbingState.FOUND_IT
                    self._detected_charset = coding_sm.get_coding_state_machine()
                    self._detected_language = coding_sm.language
                    return self.state

        # 返回状态
        return self.state
