#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

import os
import unittest2
import configparser

from alignak_app.core.utils.config import get_url_endpoint_from_icon_name, Settings


class TestUtils(unittest2.TestCase):
    """
        This file test methods of `utils.py` file.
    """

    def test_init_config(self):
        """Init Settings"""

        under_test = Settings()

        self.assertIsInstance(under_test.app_config, configparser.ConfigParser)
        self.assertIsInstance(under_test.img_config, configparser.ConfigParser)
        self.assertIsNone(under_test.settings)
        self.assertIsNone(under_test.images)
        self.assertIsNone(under_test.app_dir)
        self.assertIsNone(under_test.css_style)

        under_test.init_config()

        self.assertIsInstance(under_test.app_config, configparser.ConfigParser)
        self.assertIsInstance(under_test.img_config, configparser.ConfigParser)
        self.assertIsNotNone(under_test.settings)
        self.assertIsNotNone(under_test.images)
        self.assertIsNotNone(under_test.app_dir)
        self.assertIsNone(under_test.css_style)

    def test_app_dir_from_env(self):
        """App Workdir from Env"""

        under_test = Settings()
        under_test.init_config()

        self.assertEqual('%s/.local/alignak_app' % os.environ['HOME'], under_test.app_dir)

        os.environ['APPWORKDIR'] = '/tmp/alignak_app'

        under_test.init_config()

        self.assertEqual('/tmp/alignak_app', under_test.app_dir)

        # Reset env var for other tests
        os.environ['APPWORKDIR'] = ''

    def test_set_app_config(self):
        """Set and Get app_config"""

        under_test = Settings()
        under_test.init_config()

        # Get current url
        url_test = under_test.get_config('Alignak', 'url')
        self.assertEqual('http://demo.alignak.net', url_test)

        # Change url
        under_test.edit_setting_value('Alignak', 'url', 'http://127.0.0.1')
        new_url_test = under_test.get_config('Alignak', 'url')
        self.assertEqual('http://127.0.0.1', new_url_test)

        # Back url to normal
        under_test.edit_setting_value('Alignak', 'url', 'http://demo.alignak.net')
        last_url_test = under_test.get_config('Alignak', 'url')

        self.assertEqual('http://demo.alignak.net', last_url_test)

        bool_test = under_test.get_config('Log', 'debug', boolean=True)

        self.assertIsInstance(bool_test, bool)

    def test_get_image(self):
        """Get Image"""

        under_test = Settings()
        under_test.init_config()

        test_icon = under_test.get_image('icon')

        self.assertEqual('/home/algorys/.local/alignak_app/images/icon.svg', test_icon)

        test_icon_error = under_test.get_image('NONE')

        self.assertEqual('/home/algorys/.local/alignak_app/images/error.svg', test_icon_error)

    def test_init_css(self):
        """Init CSS"""

        under_test = Settings()
        under_test.init_config()

        self.assertIsNone(under_test.css_style)

        under_test.init_css()

        self.assertIsNotNone(under_test.css_style)
        self.assertIsInstance(under_test.css_style, str)

        # Set 'APPWORKDIR' and init again config
        os.environ['APPWORKDIR'] = '/tmp/alignak_app'
        under_test.init_config()

        under_test.init_css()

        self.assertEqual('', under_test.css_style)

        # Reset env var for other tests
        os.environ['APPWORKDIR'] = ''

    def test_get_url_endpoint_from_icon_name(self):
        """Get Url Endpoint from Icon Name"""

        under_test = get_url_endpoint_from_icon_name('services_ok')

        self.assertEqual('/table?search=ls_state:OK', under_test)

        under_test = get_url_endpoint_from_icon_name('unknown_icon')

        self.assertEqual('/table', under_test)

@@ -3407,36 +3407,25 @@
 ssertEqual('
-/home/algory
+%25
 s/.local/ali
@@ -3445,24 +3445,45 @@
 es/icon.svg'
+ %25 os.environ%5B'HOME'%5D
 , test_icon)
@@ -3569,21 +3569,23 @@
 ual(
-'/home/algory
+%0A            '%25
 s/.l
@@ -3618,16 +3618,37 @@
 ror.svg'
+ %25 os.environ%5B'HOME'%5D
 , test_i
@@ -3656,16 +3656,25 @@
 on_error
+%0A        
 )%0A%0A    d

Fix Unit Test for get_image