// 模拟 WPF 相关类型
class DependencyProperty {
    static ItemsSourceProperty = new DependencyProperty();
    static SelectedValuePathProperty = new DependencyProperty();
    static DisplayMemberPathProperty = new DependencyProperty();
}

class DataGridComboBoxColumn {
    ItemsSource: any;
    SelectedValuePath: string;
    DisplayMemberPath: string;

    protected OnPropertyChanged(e: { Property: DependencyProperty }) { }
    protected GenerateEditingElement(cell: any, dataItem: any): any { return null; }
    protected PrepareCellForEdit(editingElement: any, editingEventArgs: any): any { return null; }
    protected CommitCellEdit(editingElement: any): boolean { return false; }
}

class DataGridTextColumn { }

class ObservableCollection<T> {
    private items: T[] = [];

    get length() { return this.items.length; }

    Add(item: T) {
        this.items.push(item);
    }
}

class ComboBoxDataGrid {
    ItemsSource: any;
    SelectedValuePath: string;
    DisplayMemberPath: string;
    SelectedValue: any;
    SelectedItem: any;
    Columns = new ObservableCollection<DataGridTextColumn>();
}

class DataGridCell { }

class RoutedEventArgs {
    Source: any;
}

class DataRowView {
    DataContext: any;
    Row: { [key: string]: any };
}

class CustDataGridComboBoxColumn extends DataGridComboBoxColumn {
    private columns: ObservableCollection<DataGridTextColumn>;
    private comboBox: ComboBoxDataGrid;

    constructor() {
        super();
        this.comboBox = new ComboBoxDataGrid();
    }

    protected OnPropertyChanged(e: { Property: DependencyProperty }) {
        if (e.Property === DependencyProperty.ItemsSourceProperty) {
            this.comboBox.ItemsSource = this.ItemsSource;
        } else if (e.Property === DependencyProperty.SelectedValuePathProperty) {
            this.comboBox.SelectedValuePath = this.SelectedValuePath;
        } else if (e.Property === DependencyProperty.DisplayMemberPathProperty) {
            this.comboBox.DisplayMemberPath = this.DisplayMemberPath;
        }

        super.OnPropertyChanged(e);
    }

    get Columns() {
        if (!this.columns) {
            this.columns = new ObservableCollection<DataGridTextColumn>();
        }
        return this.columns;
    }

    protected GenerateEditingElement(cell: DataGridCell, dataItem: any) {
        if (this.comboBox.Columns.length === 0) {
            for (let i = 0; i < this.Columns.length; i++) {
                this.comboBox.Columns.Add(this.Columns[i]);
            }
        }
        return this.comboBox;
    }

    protected PrepareCellForEdit(editingElement: any, editingEventArgs: RoutedEventArgs) {
        const cell = editingEventArgs.Source as DataGridCell;
        if (cell) {
            const obj = ((editingElement as DataRowView).DataContext as DataRowView).Row[this.SelectedValuePath];
            this.comboBox.SelectedValue = obj;
        }

        return this.comboBox.SelectedItem;
    }

    protected CommitCellEdit(editingElement: any) {
        ((editingElement as DataRowView).DataContext as DataRowView).Row[this.SelectedValuePath] = this.comboBox.SelectedValue;
        return true;
    }
}

export { CustDataGridComboBoxColumn };