######################## BEGIN LICENSE BLOCK ########################
# The Original Code is Mozilla Communicator client 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 .enums import ProbingState
from .charsetprober import CharSetProber


class CharSetGroupProber(CharSetProber):
    # 初始化函数，传入语言过滤器
    def __init__(self, lang_filter=None):
        # 调用父类的初始化函数
        super(CharSetGroupProber, self).__init__(lang_filter=lang_filter)
        # 初始化活动探测器数量
        self._active_num = 0
        # 初始化探测器列表
        self.probers = []
        # 初始化最佳猜测探测器
        self._best_guess_prober = None

    # 重置函数
    def reset(self):
        # 调用父类的重置函数
        super(CharSetGroupProber, self).reset()
        # 重置活动探测器数量
        self._active_num = 0
        # 遍历探测器列表
        for prober in self.probers:
            # 如果探测器存在
            if prober:
                # 重置探测器
                prober.reset()
                # 设置探测器为活动状态
                prober.active = True
                # 活动探测器数量加一
                self._active_num += 1
        # 重置最佳猜测探测器
        self._best_guess_prober = None

    # 获取字符集名称的属性函数
    @property
    def charset_name(self):
        # 如果最佳猜测探测器不存在
        if not self._best_guess_prober:
            # 调用获取置信度函数
            self.get_confidence()
            # 如果最佳猜测探测器仍然不存在
            if not self._best_guess_prober:
                # 返回None
                return None
        # 返回最佳猜测探测器的字符集名称
        return self._best_guess_prober.charset_name

    # 获取语言的属性函数
    @property
    def language(self):
        # 如果最佳猜测探测器不存在
        if not self._best_guess_prober:
            # 调用获取置信度函数
            self.get_confidence()
            # 如果最佳猜测探测器仍然不存在
            if not self._best_guess_prober:
                # 返回None
                return None
        # 返回最佳猜测探测器的语言
        return self._best_guess_prober.language

    # 接收字节字符串的函数
    def feed(self, byte_str):
        # 遍历探测器列表
        for prober in self.probers:
            # 如果探测器不存在
            if not prober:
                # 跳过
                continue
            # 如果探测器不是活动状态
            if not prober.active:
                # 跳过
                continue
            # 调用探测器接收字节字符串的函数
            state = prober.feed(byte_str)
            # 如果探测器返回的状态不是FOUND_IT
            if not state:
                # 跳过
                continue
            # 如果探测器返回的状态是FOUND_IT
            if state == ProbingState.FOUND_IT:
                # 设置最佳猜测探测器为当前探测器
                self._best_guess_prober = prober
                # 返回当前探测器的状态
                return self.state
            # 如果探测器返回的状态是NOT_ME
            elif state == ProbingState.NOT_ME:
                # 设置探测器为非活动状态
                prober.active = False
                # 活动探测器数量减一
                self._active_num -= 1
                # 如果活动探测器数量小于等于0
                if self._active_num <= 0:
                    # 设置当前探测器的状态为NOT_ME
                    self._state = ProbingState.NOT_ME
                    # 返回当前探测器的状态
                    return self.state
        # 返回当前探测器的状态
        return self.state

    # 获取置信度的函数
    def get_confidence(self):
        # 获取当前探测器的状态
        state = self.state
        # 如果当前探测器的状态是FOUND_IT
        if state == ProbingState.FOUND_IT:
            # 返回0.99
            return 0.99
        # 如果当前探测器的状态是NOT_ME
        elif state == ProbingState.NOT_ME:
            # 返回0.01
            return 0.01
        # 初始化最佳置信度
        best_conf = 0.0
        # 重置最佳猜测探测器
        self._best_guess_prober = None
        # 遍历探测器列表
        for prober in self.probers:
            # 如果探测器不存在
            if not prober:
                # 跳过
                continue
            # 如果探测器不是活动状态
            if not prober.active:
                self.logger.debug('%s not active', prober.charset_name)
                continue
            conf = prober.get_confidence()
            self.logger.debug('%s %s confidence = %s', prober.charset_name, prober.language, conf)
            if best_conf < conf:
                best_conf = conf
                self._best_guess_prober = prober
        if not self._best_guess_prober:
            return 0.0
        return best_conf
