// 定义模拟类型
enum ListSortDirection {
  Ascending,
  Descending
}

interface GroupSortGridViewColumn {
  SortPropertyName: string;
  GroupPropertyName: string;
  IsDefaultSortColumn: boolean;
  HeaderContainerStyle: any;
}

interface GridView {
  Columns: GroupSortGridViewColumn[];
}

interface ICollectionView {
  SortDescriptions: any[];
  GroupDescriptions: any[];
  Refresh(): void;
}

// 假设的 CollectionViewSource 类
class CollectionViewSource {
  static GetDefaultView(obj: any): ICollectionView {
    return {
      SortDescriptions: [],
      GroupDescriptions: [],
      Refresh: () => {}
    };
  }
}

// 假设的 Style 类型
type Style = any;

export class GroupSortListView {
  lastSortedOnColumn: GroupSortGridViewColumn | null = null;
  lastDirection: ListSortDirection = ListSortDirection.Ascending;
  groupMenuItem: any;
  private inGroupingMode: boolean = false;
  private groupName: string = '';

  // 依赖属性模拟
  GroupSortListViewColumnHeaderSortedAscendingStyle: string = '';
  GroupSortListViewColumnHeaderSortedDescendingStyle: string = '';
  GroupSortListViewColumnHeaderNotSortedStyle: string = '';

  View: GridView | null = null;
  ItemsSource: any;

  constructor() {
    this.init();
  }

  private init() {
    if (this.View) {
      let groupSortGridViewColumn: GroupSortGridViewColumn | null = null;
      for (const gridViewColumn of this.View.Columns) {
        if (gridViewColumn.IsDefaultSortColumn) {
          groupSortGridViewColumn = gridViewColumn;
          break;
        }
      }
      if (groupSortGridViewColumn) {
        this.lastSortedOnColumn = groupSortGridViewColumn;
        this.Sort(groupSortGridViewColumn.SortPropertyName, ListSortDirection.Ascending);
        if (this.GroupSortListViewColumnHeaderSortedAscendingStyle) {
          groupSortGridViewColumn.HeaderContainerStyle = this.findResource(this.GroupSortListViewColumnHeaderSortedAscendingStyle);
        }
      }
    }
  }

  private findResource(name: string): Style | null {
    // 模拟查找资源
    return null;
  }

  public Sort(sortBy: string, direction: ListSortDirection) {
    this.lastDirection = direction;
    const dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);
    dataView.SortDescriptions = [];
    dataView.SortDescriptions.push({ propertyName: sortBy, direction });
    dataView.Refresh();
  }

  public Group(groupBy: string, obj: any) {
    const dataView = CollectionViewSource.GetDefaultView(obj);
    if (!this.GroupExists(groupBy, dataView)) {
      dataView.GroupDescriptions.push({ propertyName: groupBy });
      dataView.Refresh();
    }
  }

  private GroupExists(groupName: string, dataCollection: ICollectionView): boolean {
    return this.FindGroup(groupName, dataCollection) !== null;
  }

  private FindGroup(groupName: string, dataCollection: ICollectionView): any {
    for (const group of dataCollection.GroupDescriptions) {
      if (group.propertyName === groupName) {
        return group;
      }
    }
    return null;
  }

  private RemoveGroup(groupName: string) {
    const dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);
    const group = this.FindGroup(groupName, dataView);
    if (group) {
      const index = dataView.GroupDescriptions.indexOf(group);
      dataView.GroupDescriptions.splice(index, 1);
      dataView.Refresh();
    }
  }

  // 模拟鼠标事件处理
  public handleColumnHeaderMouseDown(column: GroupSortGridViewColumn) {
    let direction: ListSortDirection;
    let newSortColumn = false;
    if (
      !this.lastSortedOnColumn ||
      !this.lastSortedOnColumn.SortPropertyName ||
      column.SortPropertyName.toLowerCase() !== this.lastSortedOnColumn.SortPropertyName.toLowerCase()
    ) {
      newSortColumn = true;
      direction = ListSortDirection.Ascending;
    } else {
      direction = this.lastDirection === ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
    }

    this.Sort(column.SortPropertyName, direction);

    if (direction === ListSortDirection.Ascending) {
      column.HeaderContainerStyle = this.GroupSortListViewColumnHeaderSortedAscendingStyle
        ? this.findResource(this.GroupSortListViewColumnHeaderSortedAscendingStyle)
        : null;
    } else {
      column.HeaderContainerStyle = this.GroupSortListViewColumnHeaderSortedDescendingStyle
        ? this.findResource(this.GroupSortListViewColumnHeaderSortedDescendingStyle)
        : null;
    }

    if (newSortColumn && this.lastSortedOnColumn) {
      this.lastSortedOnColumn.HeaderContainerStyle = this.GroupSortListViewColumnHeaderNotSortedStyle
        ? this.findResource(this.GroupSortListViewColumnHeaderNotSortedStyle)
        : null;
    }
    this.lastSortedOnColumn = column;
  }

  public handleColumnHeaderMouseMove(column: GroupSortGridViewColumn) {
    const groupName = column.GroupPropertyName;
    if (groupName) {
      this.groupName = groupName;
      this.Group(groupName, this.ItemsSource);
    }
  }

  // 模拟上下文菜单点击
  public handleGroupMenuItemClick() {
    if (this.inGroupingMode) {
      this.inGroupingMode = false;
    } else {
      this.RemoveGroup(this.groupName);
      this.inGroupingMode = true;
    }
  }
}