# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""Test Code_view module."""

import pytest
import pytest_check as check

from msticpy.common.utility import is_ipython
from msticpy.vis import code_view

__author__ = "Ian Hellen"

# pylint: disable=redefined-outer-name

_INPUT_TEXT = r"""
if($psversiontable.psversion.major -ge 3)
{
    $myvar=[ref].assembly.gettype('system.somelib')."getfield"('cachedtest','nonpublic,static')
    if($myvar)
    {
        $var2=$myvar.getvalue($null)
        $val=[collections.generic.dictionary[string,system.object]]::new()
        $val.add('test2',0)
        $val.add('test1',0)
        $var2['hkey_local_machine\software\someapp']=$val
    }
    else
    {
        [scriptblock]."getfield"('tes','nt3,param').setvalue($null,(new-object sample.first.item))
    }
    $ref=[ref].assembly.gettype('system.test.test2.utils')
    $ref.getfield('testinitfailed','nonreal,dynamic').setvalue($null,$true)
}
"""

_CORE_HTML = """
<span class="nv">$myvar</span><span class="p">=</span><span class="no">
[ref]</span><span class="p">.</span><span class="n">assembly</span><span class="p">
.</span><span class="n">gettype</span><span class="p">(</span><span class="s1">
&#39;system.somelib&#39;</span><span class="p">).</span><span class="s2">
&quot;getfield&quot;</span><span class="p">(</span><span class="s1">
&#39;cachedtest&#39;</span><span class="p">,</span><span class="s1">
&#39;nonpublic,static&#39;</span><span class="p">)</span>
"""

_HTML_STYLE_HDR = """
<html>
<head>
  <title></title>
  <meta http-equiv="content-type" content="text/html; charset=None">
  <style type="text/css">
/*
generated by Pygments <https://pygments.org/>
"""


def _core_html():
    return _CORE_HTML.replace("\n", "")


_CODE_VIEW_TESTS = [
    ("powershell", None, None, (_core_html(), _HTML_STYLE_HDR)),
    ("powershell", None, False, (_core_html(), None)),
    ("powershell", None, True, (_core_html(), _HTML_STYLE_HDR)),
    ("powershell", "default", None, (_core_html(), _HTML_STYLE_HDR)),
    ("powershell", "stata-dark", True, (_core_html(), _HTML_STYLE_HDR)),
]


@pytest.mark.parametrize("lang, style, full, expected", _CODE_VIEW_TESTS)
def test_to_html(lang, style, full, expected):
    """Test to_html function."""
    core_html, style_hdr = expected
    kwargs = {"language": lang, "style": style, "full": full}
    if style is None:
        del kwargs["style"]
    if full is None:
        del kwargs["full"]

    html = code_view.to_html(_INPUT_TEXT, **kwargs)
    print(html)
    check.is_in(core_html, html)
    if full:
        check.is_in(style_hdr, html)


def test_list_pygments_styles():
    """Test expected styles in pygments styles."""
    styles = code_view.list_pygments_styles()
    check.is_in("stata-dark", styles)
    check.is_in("default", styles)


@pytest.mark.parametrize("lang, style, full, expected", _CODE_VIEW_TESTS)
def test_display_code(lang, style, full, expected):
    """Simple test for display_code."""
    del expected
    kwargs = {"language": lang, "style": style, "full": full}
    if style is None:
        del kwargs["style"]
    if full is None:
        del kwargs["full"]

    handle = code_view.display_html(_INPUT_TEXT, **kwargs)
    if is_ipython(notebook=True):
        check.is_not_none(handle)
