#  Copyright 2024-     Robot Framework Foundation
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import unittest
import os
import pytest
from random import randint
from pytest import MonkeyPatch

DISPLAY = os.getenv('DISPLAY')
if not DISPLAY:  # Avoid failing unit tests in system without X11
    pytest.skip("Skipped because of missing DISPLAY", allow_module_level=True)
import wx
from wx.lib.agw.aui import AuiManager

from robotide.robotapi import (TestDataDirectory, TestCaseFile, ResourceFile,
                               TestCase, UserKeyword)
from robotide.spec.librarymanager import LibraryManager
from robotide.ui.mainframe import ActionRegisterer, ToolBar, start_external_app
from robotide.ui.actiontriggers import MenuBar, ShortcutRegistry
from robotide.application import Project
from robotide.application.pluginloader import PluginLoader
from robotide.controller.filecontrollers import (TestDataDirectoryController, ResourceFileController)
from utest.resources import FakeSettings
from robotide.editor.texteditor import TextEditorPlugin
from robotide.log import LogPlugin
from robotide.ui import mainframe
from robotide.publish import PUBLISHER
from robotide.preferences import Settings
from robotide.ui.treeplugin import Tree
from robotide.namespace.namespace import Namespace


app = wx.App()

class FakePlugin():
    settings = {'foreground':'black',
                'background':'white',
                'secondary foreground':'blue',
                'secondary background':'gray',
                'font face':'',
                'font size':11,
                'own colors':False }
    html_font_size = 11
    general_settings = {'foreground':'black',
                'background':'white',
                'secondary foreground':'blue',
                'secondary background':'gray',
                'apply to panels':False}

    def __init__(self):
        self.settings = Settings(user_path=os.path.join(os.path.dirname(__file__), 'fake.cfg'))
        self.settings.set('foreground', 'black')
        self.settings.set('background', 'white')
        self.settings.set('secondary foreground', 'blue')
        self.settings.set('secondary background', 'gray')
        self.settings.set('font_size', '11')


class MyPlatform():
    def startswith(self):
        plt = ['linux', 'darwin', 'win32', 'something']
        return plt[randint(0, 3)] == self

class _BaseDialogTest(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        self.app.settings = FakeSettings()
        font_size = self.app.settings['General'].get('font size', 12)
        font_face = self.app.settings['General'].get('font face', 'Helvetica')
        self.app.fontinfo = wx.FontInfo(font_size).FaceName(font_face).Bold(False)
        self.app.namespace = Namespace(FakeSettings())
        self.model = self._create_model()
        self.app.fileexplorerplugin = FakePlugin
        self.frame = mainframe.RideFrame(self.app, self.model)
        self.app.frame = self.frame
        txtplugin = TextEditorPlugin
        logplugin = LogPlugin
        plugins_dir = [os.path.join(os.path.dirname(__file__), 'plugins_for_loader')]
        self.loader = PluginLoader(self.app, plugins_dir, [txtplugin, logplugin])
        self.app.get_plugins = lambda: self.loader.plugins
        self.frame.tree = Tree(self.frame, ActionRegisterer(AuiManager(self.frame),
                                                            MenuBar(self.frame), ToolBar(self.frame),
                                                            ShortcutRegistry(self.frame)), self.app.settings)
        # self.frame.Show()

    def tearDown(self):
        PUBLISHER.unsubscribe_all()
        self.app.ExitMainLoop()
        self.app.Destroy()
        self.app = None
        # app.MainLoop()  # With this here, there is no Segmentation fault

    def _create_model(self):
        suite = self._create_directory_suite('/top_suite')
        suite.children = [self._create_file_suite('sub_suite_%d.robot' % i)
                          for i in range(3)]
        res = ResourceFile()
        res.source = 'resource.robot'
        res.keyword_table.keywords.append(UserKeyword(res, 'Resource Keyword', ['en']))
        library_manager = LibraryManager(':memory:')
        library_manager.create_database()
        model = Project(self.app.namespace, library_manager=library_manager)
        model.controller = TestDataDirectoryController(suite)
        rfc = ResourceFileController(res, project=model)
        model.resources.append(rfc)
        model.insert_into_suite_structure(rfc)
        return model

    def _create_directory_suite(self, source):
        return self._create_suite(TestDataDirectory, source, is_dir=True)

    def _create_file_suite(self, source):
        suite = self._create_suite(TestCaseFile, source)
        suite.testcase_table.tests = [TestCase(suite, '%s Fake Test %d' % (suite.name, i)) for i in range(16)]
        return suite

    @staticmethod
    def _create_suite(suite_class, source, is_dir=False):
        suite = suite_class()
        suite.source = source
        if is_dir:
            suite.directory = source
        suite.keyword_table.keywords = [UserKeyword(suite.keyword_table, '%s Fake UK %d' % (suite.name, i),
                                                    ['en'])
            for i in range(5)]
        return suite


class TestMainFrame(_BaseDialogTest):

    def test_show_mainframe(self):
       self.frame.Show()
       wx.CallLater(2000, self.app.ExitMainLoop)
       # self.app.MainLoop()

    def test_show_plugins_manager(self):
        # self.frame.Show()
        plugins = self.loader.plugins
        self.frame._plugin_manager.show(plugins)
        wx.CallLater(5000, self.app.ExitMainLoop)
        wx.CallLater(4000, self.frame._plugin_manager._panel.Close)
        # self.app.MainLoop()

    def test_start_external_app(self):
        import sys, subprocess
        self._platform = ""

        def my_call(*argument, **options):
            _ = options
            value = " ".join(argument[0])
            # print(f'DEBUG: Calling suprocess.call("{value}")')

        with MonkeyPatch().context() as m:
            m.setattr(sys, 'platform', MyPlatform)
            m.setattr(sys.platform, 'startswith', MyPlatform.startswith)
            m.setattr(subprocess, 'call', my_call)

            for _ in range(16):  # Hoping to cover all 4 cases
                start_external_app(__file__)

    def test_on_auto_save_restarts_timer_after_save(self):
        calls = []

        class DirtyController:
            @staticmethod
            def is_dirty():
                return True

        class FakeBeforeSaving:
            def __init__(self, auto=True):
                self.auto = auto

            def publish(self):
                calls.append("publish")

        def restart_timer():
            calls.append("restart")

        def fake_call_after(callback, *args, **kwargs):
            calls.append(("callafter", callback, args, kwargs))

        self.frame.controller = DirtyController()
        with MonkeyPatch().context() as m:
            m.setattr(mainframe, 'RideBeforeSaving', FakeBeforeSaving)
            m.setattr(wx, 'CallAfter', fake_call_after)
            m.setattr(self.frame, 'save_all', lambda: calls.append("save_all"))
            m.setattr(self.frame, 'SetStatusText', lambda text: calls.append(("status", text)))
            m.setattr(self.frame, '_start_auto_save_timer', restart_timer)
            m.setattr(self.frame, 'is_user_idle', lambda: True)

            self.frame._on_auto_save(object())

        self.assertEqual(
            calls,
            [
                "publish",
                "save_all",
                ("callafter", restart_timer, (), {}),
                ("status", "Auto-saved all files")
            ],
        )


if __name__ == '__main__':
    unittest.main()
    app.Destroy()
