"""Subclass of AstExplorerPane, which is generated by wxFormBuilder."""

import ast
import wx
import Tool
import activity.files.ui.pane as pane
import res
from ctx import theContext as context
import wx.propgrid as pg


# Implementing AstExplorerPane
class ToolAstExplorerPane(Tool.AstExplorerPane):
    """Ast Explorer pane"""
    def __init__(self, parent, item, data):
        """Init"""
        self._path = item._file
        self._treeid = None
        self._data = data
        Tool.AstExplorerPane.__init__(self, parent)
        self._item = item
        self.m_editor = pane.FilePythonPane(self.m_splitter3, context.frame, self._item)
        self.m_editor.SetReadOnly(True)
        self.m_splitter3.SplitVertically(self.m_panel32, self.m_editor, 0)
        self.m_tree.AssignImageList(res.GetBitmapImageList())
        self.MapIcons()
        self.FillPath()

    def MapIcons(self):
        """Create a image map"""
        self._iconmap = {}
        self._iconmap[ast.Module] = res.GetBitmapIndex('module')
        self._iconmap[ast.Expr] = res.GetBitmapIndex('expr')
        self._iconmap[ast.Import] = res.GetBitmapIndex('import')
        self._iconmap[ast.ImportFrom] = res.GetBitmapIndex('import')
        self._iconmap[ast.Str] = res.GetBitmapIndex('str')
        self._iconmap[ast.alias] = res.GetBitmapIndex('alias')
        self._iconmap[ast.Assign] = res.GetBitmapIndex('assign')
        self._iconmap[ast.Name] = res.GetBitmapIndex('name')
        self._iconmap[ast.BinOp] = res.GetBitmapIndex("binop")
        self._iconmap[ast.Store] = res.GetBitmapIndex("store")
        self._iconmap[ast.Load] = res.GetBitmapIndex("load")
        self._iconmap[ast.Attribute] = res.GetBitmapIndex("attribute")
        self._iconmap[ast.Add] = res.GetBitmapIndex("add")
        self._iconmap[ast.Num] = res.GetBitmapIndex("num")
        self._iconmap[ast.ClassDef] = res.GetBitmapIndex("classdef")
        self._iconmap[ast.FunctionDef] = res.GetBitmapIndex("functiondef")
        self._iconmap[ast.arguments] = res.GetBitmapIndex("arguments")
        self._iconmap[ast.Param] = res.GetBitmapIndex("param")
        self._iconmap[ast.Call] = res.GetBitmapIndex("call")
        self._iconmap[ast.Dict] = res.GetBitmapIndex("dict")
        self._iconmap[ast.Return] = res.GetBitmapIndex("return")
        self._iconmap[ast.ListComp] = res.GetBitmapIndex("listcomp")
        self._iconmap[ast.Tuple] = res.GetBitmapIndex("tuple")
        self._iconmap[ast.comprehension] = res.GetBitmapIndex("comprehension")
        self._iconmap[ast.Global] = res.GetBitmapIndex("global")
        self._iconmap[ast.For] = res.GetBitmapIndex("for")
        self._iconmap[ast.keyword] = res.GetBitmapIndex("keyword")
        self._iconmap[ast.If] = res.GetBitmapIndex("if")
        self._iconmap[ast.BoolOp] = res.GetBitmapIndex("bool")
        self._iconmap[ast.And] = res.GetBitmapIndex("and")
        self._iconmap[ast.Compare] = res.GetBitmapIndex("compare")
        self._iconmap[ast.In] = res.GetBitmapIndex("in")
        self._iconmap[ast.Subscript] = res.GetBitmapIndex("subscript")
        self._iconmap[ast.Index] = res.GetBitmapIndex("index")
        self._iconmap[ast.UnaryOp] = res.GetBitmapIndex("unary")
        self._iconmap[ast.Not] = res.GetBitmapIndex("not")
        self._iconmap[ast.Eq] = res.GetBitmapIndex("eq")
        self._iconmap[ast.NotEq] = res.GetBitmapIndex("not_eq")
        self._iconmap[ast.Delete] = res.GetBitmapIndex("delete")
        self._iconmap[ast.Del] = res.GetBitmapIndex("del")
        self._iconmap[ast.Sub] = res.GetBitmapIndex("sub")
        self._iconmap[ast.NotIn] = res.GetBitmapIndex("not_in")
        self._iconmap[ast.GtE] = res.GetBitmapIndex("gte")
        self._iconmap[ast.LtE] = res.GetBitmapIndex("lte")
        self._iconmap[ast.Gt] = res.GetBitmapIndex("gt")
        self._iconmap[ast.Lt] = res.GetBitmapIndex("lt")
        self._iconmap[ast.Is] = res.GetBitmapIndex("is")
        self._iconmap[ast.IsNot] = res.GetBitmapIndex("is_not")
        self._iconmap[ast.FloorDiv] = res.GetBitmapIndex("floor_div")
        self._iconmap[ast.Continue] = res.GetBitmapIndex("continue")
        self._iconmap[ast.TryExcept] = res.GetBitmapIndex("try")
        self._iconmap[ast.Or] = res.GetBitmapIndex("or")
        self._iconmap[ast.ExceptHandler] = res.GetBitmapIndex("except")
        self._iconmap[ast.Print] = res.GetBitmapIndex("print")
        self._iconmap[ast.List] = res.GetBitmapIndex("list")

    def process_node(self, parent_item, node):
        """This is the root of process analysing"""
        data = wx.TreeItemData(node)
        tree_item = self.m_tree.AppendItem(parent_item, str(type(node)), data=data)
        if type(node) in self._iconmap:
            z = self._iconmap[type(node)]
            self.m_tree.SetItemImage(tree_item, z, wx.TreeItemIcon_Normal)
        #else:
        #    print type(node)
        for child_node in ast.iter_child_nodes(node):
            self.process_node(tree_item, child_node)

    def FillPath(self):
        """Fill the tree with the ast info"""
        self.m_tree.AddRoot('nodes')
        self.process_node(self.m_tree.GetRootItem(), self._data)

    def OnSelectItem(self, event):
        """Process select item"""
        item = self.m_tree.GetSelection()
        if not item.IsOk():
            return
        data = self.m_tree.GetItemData(item)
        obj = data.GetData()
        if hasattr(obj, 'to_lineno'):
            #self.m_editor.GotoLine(obj.lineno - 1, True)
            self.m_editor.Select(obj.lineno - 1, obj.col_offset, obj.to_lineno - 1, obj.to_col_offset)
        self.m_propertyGrid.Clear()
        for k in obj.__dict__.keys():
            prop = pg.NewStringProperty(k, k, str(obj.__dict__[k]))
            self.m_propertyGrid.Append(prop)
            self.m_propertyGrid.SetPropertyReadOnly(k)

