# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015

# This file is part of iwla

# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla.  If not, see <http://www.gnu.org/licenses/>.
#

from iwla import IWLA
from iplugin import IPlugin
from display import *

import awstats_data

"""
Display hook

Add operating systems statistics

Plugin requirements :
    post_analysis/operating_systems

Conf values needed :
    create_families_page*

Output files :
    OUTPUT_ROOT/year/month/index.html

Statistics creation :
    None

Statistics update :
    None

Statistics deletion :
    None
"""

class IWLADisplayTopOperatingSystems(IPlugin):
    def __init__(self, iwla):
        super(IWLADisplayTopOperatingSystems, self).__init__(iwla)
        self.API_VERSION = 1
        self.requires = ['IWLAPostAnalysisOperatingSystems']

    def load(self):
        self.icon_path = self.iwla.getConfValue('icon_path', '/')
        self.create_families_page = self.iwla.getConfValue('create_families_page_page', True)
        self.icon_names = {v:k for (k, v) in awstats_data.operating_systems_family.items()}

        return True

    def hook(self):
        display = self.iwla.getDisplay()
        os_families = self.iwla.getMonthStats()['os_families']
        os_families = sorted(os_families.items(), key=lambda t: t[1], reverse=True)
        operating_systems = self.iwla.getMonthStats()['operating_systems']
        operating_systems = sorted(operating_systems.items(), key=lambda t: t[1], reverse=True)

        # All in a page
        if self.create_families_page:
            title = createCurTitle(self.iwla, u'All Operating Systems')
            filename = 'operating_systems.html'
            path = self.iwla.getCurDisplayPath(filename)

            page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
            table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Operating Systems'), ['', self.iwla._(u'Operating System'), self.iwla._(u'Entrance')])
            table.setColsCSSClass(['', '', 'iwla_hit'])
            for (os_name, entrance) in operating_systems:
                icon = '<img alt="%s icon" src="/%s/os/%s.png"/>' % (os_name, self.icon_path, os_name)
                table.appendRow([icon, os_name, entrance])
            page.appendBlock(table)

            display.addPage(page)

        # Families in index
        title = self.iwla._(u'Operating Systems')
        if self.create_families_page:
            link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'Details'))
            title = '%s - %s' % (title, link)

        index = self.iwla.getDisplayIndex()
        
        table = display.createBlock(DisplayHTMLBlockTable, title, ['', self.iwla._(u'Operating System'), self.iwla._(u'Entrance')])        
        table.setColsCSSClass(['', '', 'iwla_hit'])
        for (family, entrance) in os_families:
            icon = '<img alt="%s icon" src="/%s/os/%s.png"/>' % (self.icon_names[family], self.icon_path, self.icon_names[family])
            table.appendRow([icon, family, entrance])
        table.computeRatio(2)
        index.appendBlock(table)