"""
Copyright (c) 2020 The Orbit Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
"""

from pywinauto.base_wrapper import BaseWrapper
from pywinauto.controls.uiawrapper import UIAWrapper, UIAElementInfo

from core.orbit_e2e import find_control


class Track:
    def __init__(self, control: BaseWrapper):
        self._container = control
        self._name = control.texts()[0]
        self._title = find_control(control, 'TabItem')
        self._callstacks = find_control(control, 'Pane', 'Callstacks', raise_on_failure=False)
        self._thread_states = find_control(control, 'Pane', 'ThreadState', raise_on_failure=False)
        self._tracepoints = find_control(control, 'Pane', 'Tracepoints', raise_on_failure=False)
        self._timers = find_control(control, 'Pane', 'Timers', raise_on_failure=False)
        self._triangle_toggle = find_control(control, 'Button', 'TriangleToggle', raise_on_failure=False)

    container = property(lambda self: self._container)
    title = property(lambda self: self._title)
    callstacks = property(lambda self: self._callstacks)
    thread_states = property(lambda self: self._thread_states)
    tracepoints = property(lambda self: self._tracepoints)
    timers = property(lambda self: self._timers)
    triangle_toggle = property(lambda self: self._triangle_toggle)
    name = property(lambda self: self._name)


class DataViewPanel:
    def __init__(self, control: BaseWrapper):
        self._panel = control
        self._table = find_control(control, "Tree", "DataView")
        self._refresh_button = find_control(control, "Button", "Refresh", raise_on_failure=False)
        self._filter = find_control(control,  "Edit", "Filter")

    panel = property(lambda self: self._panel)
    table = property(lambda self: self._table)
    refresh_button = property(lambda self: self._refresh_button)
    filter = property(lambda self: self._filter)

    def get_row_count(self):
        return self.table.iface_grid.CurrentRowCount

    def get_column_count(self):
        return self.table.iface_grid.CurrentColumnCount

    def get_item_at(self, row: int, col: int) -> BaseWrapper:
        """
        This uses the low-level API to return the element at row x col.
        pywinauto does not seem to provide row/col access for TreeView, even though the element supports the grid pattern.
        Using get_item does not return items that are currently hidden. Unfortunately this also means that columns
        hidden due to scrolling are NOT handled correctly, and get_item(col + row * col_count) does not always yield
        the correct result...

        We really should not need to use the low-level API like this.
        """
        element_info = UIAElementInfo(self.table.iface_grid.GetItem(row, col))
        wrapper = UIAWrapper(element_info)
        return wrapper

    def find_first_item_row(self, text: str, column: int, partial_match=False) -> int:
        row_count = self.get_row_count()
        for i in range(row_count):
            item = self.get_item_at(i, column)
            item_text = item.texts()[0]
            if item_text == text or partial_match and text in item_text:
                return i

        return None

@@ -1445,21 +1445,13 @@
 ass 
-DataViewPanel
+Table
 :%0A  
@@ -1498,38 +1498,8 @@
 r):%0A
-        self._panel = control%0A
     
@@ -1520,420 +1520,15 @@
 e = 
-find_
 control
-(control, %22Tree%22, %22DataView%22)%0A        self._refresh_button = find_control(control, %22Button%22, %22Refresh%22, raise_on_failure=False)%0A        self._filter = find_control(control,  %22Edit%22, %22Filter%22)%0A%0A    panel = property(lambda self: self._panel)%0A    table = property(lambda self: self._table)%0A    refresh_button = property(lambda self: self._refresh_button)%0A    filter = property(lambda self: self._filter)
 %0A%0A  
@@ -1566,32 +1566,33 @@
     return self.
+_
 table.iface_grid
@@ -1657,24 +1657,25 @@
 return self.
+_
 table.iface_
@@ -2356,16 +2356,17 @@
 fo(self.
+_
 table.if
@@ -2545,16 +2545,24 @@
 ) -%3E int
+ or None
 :%0A      
@@ -2837,8 +2837,1020 @@
 rn None%0A
+%0A%0Aclass DataViewPanel:%0A    def __init__(self, control: BaseWrapper):%0A        self._panel = control%0A        self._table = find_control(control, %22Tree%22, %22DataView%22)%0A        self._table_obj = Table(self._table)%0A        self._refresh_button = find_control(control, %22Button%22, %22Refresh%22, raise_on_failure=False)%0A        self._filter = find_control(control,  %22Edit%22, %22Filter%22)%0A%0A    panel = property(lambda self: self._panel)%0A    table = property(lambda self: self._table)%0A    refresh_button = property(lambda self: self._refresh_button)%0A    filter = property(lambda self: self._filter)%0A%0A    def get_row_count(self):%0A        return self._table_obj.get_row_count()%0A%0A    def get_column_count(self):%0A        return self._table_obj.get_column_count()%0A%0A    def get_item_at(self, row: int, col: int) -%3E BaseWrapper:%0A        return self._table_obj.get_item_at(row, col)%0A%0A    def find_first_item_row(self, text: str, column: int, partial_match=False) -%3E int:%0A        return self.find_first_item_row(text, column, partial_match)%0A

Add "Table" class to e2e common_controls.py