// ****************************************************** //
// Originally generated by scripts\controls.py, but now
// maintained manually.
// ****************************************************** //
#include "pch.h"
#include "_winui.h"

std::wstring default_winrt_repr(const IInspectable &value) {
    if (!value)
        return L"None";
    std::wstringstream s;
    s << L"<" << std::wstring{winrt::get_class_name(value)};
    auto str{value.try_as<winrt::Windows::Foundation::IStringable>()};
    if (str) {
        s << L"(" << std::wstring{str.ToString()} << L")";
    }
    s << L">";
    return s.str();
}


std::wstring cvt<winrt::hstring>::repr() {
    // TODO: Full escaping
    return L"\"" + std::wstring{value} + L"\"";
}


PYBIND11_EMBEDDED_MODULE(_winui_Windows_Foundation, m) {
    py::class_<IInspectable, ::pywinui::holder<IInspectable>>(m, "Windows.Foundation.IInspectable")
        .def("__repr__", [](const IInspectable& _self) { return default_winrt_repr(_self); })
        .def("as_", [](const IInspectable& _self, const char *type) {
            std::string module_name{"_winui."};
            module_name += type;
            module_name.erase(module_name.find_last_of('.'));
            std::replace(std::begin(module_name), std::end(module_name), '.', '_');
            return py::module_::import(module_name.c_str()).attr(type)(::pywinui::hold(_self));
        })
    ;

    py::class_<Deferral, ::pywinui::holder<Deferral>, IInspectable>(m, "Windows.Foundation.Deferral")
        .def(py::init([](const IInspectable &unk) { return ::pywinui::hold(unk.as<Deferral>()); }))
        .def("__repr__", [](const Deferral &_self) { return default_winrt_repr(_self); })
        .def("Complete", [](Deferral &_self) { _self.Complete(); })
    ;

    py::class_<IAsyncOperation<winrt::hstring>, ::pywinui::holder<IAsyncOperation<winrt::hstring>>>(m, "Windows.Foundation.IAsyncOperation<winrt.hstring")
        .def("__repr__", [](const IAsyncOperation<winrt::hstring> &_self) { return default_winrt_repr(_self); })
        .def("GetResults", [](const IAsyncOperation<winrt::hstring> &_self) { return ::pywinui::hold(_self.GetResults()); })
        .def("Completed", [](IAsyncOperation<winrt::hstring> &_self, py::object on_complete) { _self.Completed(asyncop_completer<winrt::hstring>(on_complete)); })
    ;

    py::class_<winrt::Windows::Foundation::Rect>(m, "Windows.Foundation.Rect")
        .def(py::init<>())
        .def(py::init([](double x, double y, double width, double height) {
            return winrt::Windows::Foundation::Rect((float)x, (float)y, (float)width, (float)height);
        }))
        .def_readwrite("X", &winrt::Windows::Foundation::Rect::X)
        .def_readwrite("Y", &winrt::Windows::Foundation::Rect::Y)
        .def_readwrite("Width", &winrt::Windows::Foundation::Rect::Width)
        .def_readwrite("Height", &winrt::Windows::Foundation::Rect::Height)
        .def("__getitem__", [](const winrt::Windows::Foundation::Rect &rect, int index) {
            switch (index) {
            case 0: return rect.X;
            case 1: return rect.Y;
            case 2: return rect.Width;
            case 3: return rect.Height;
            default:
                throw py::index_error();
            }
        })
        .def("__setitem__", [](winrt::Windows::Foundation::Rect &rect, int index, double v) {
            switch (index) {
            case 0: rect.X = (float)v;
            case 1: rect.Y = (float)v;
            case 2: rect.Width = (float)v;
            case 3: rect.Height = (float)v;
            default:
                throw py::index_error();
            }
        })
    ;
}
