/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <swtypes.hxx>
#include "selectdbtabledialog.hxx"
#include "dbtablepreviewdialog.hxx"
#include <svtools/simptabl.hxx>
#include <vcl/treelistentry.hxx>
#include <osl/diagnose.h>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/sdb/XQueriesSupplier.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <com/sun/star/sdbc/XDataSource.hpp>

#include <unomid.h>

#include <dbui.hrc>
#include <strings.hrc>

using namespace ::com::sun::star;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdb;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::beans;

class SwAddressTable : public SvSimpleTable
{
public:
    explicit SwAddressTable(SvSimpleTableContainer& rParent);
    void InsertHeaderItem(sal_uInt16 nColumn, const OUString& rText);
    virtual void Resize() override;
    void setColSizes();
};

SwAddressTable::SwAddressTable(SvSimpleTableContainer& rParent)
    : SvSimpleTable(rParent, 0)
{
    SetSpaceBetweenEntries(3);
    SetSelectionMode(SelectionMode::Single);
    SetDragDropMode(DragDropMode::NONE);
    EnableAsyncDrag(false);
}

void SwAddressTable::InsertHeaderItem(sal_uInt16 nColumn, const OUString& rText)
{
    GetTheHeaderBar().InsertItem( nColumn, rText, 0, HeaderBarItemBits::LEFT | HeaderBarItemBits::VCENTER );
}

void SwAddressTable::Resize()
{
    SvSimpleTable::Resize();
    setColSizes();
}

void SwAddressTable::setColSizes()
{
    HeaderBar &rHB = GetTheHeaderBar();
    if (rHB.GetItemCount() < 2)
        return;

    long nWidth = rHB.GetSizePixel().Width();
    nWidth /= 2;

    long nTabs[2] = { 0, nWidth };
    SvSimpleTable::SetTabs(SAL_N_ELEMENTS(nTabs), nTabs, MapUnit::MapPixel);
}

SwSelectDBTableDialog::SwSelectDBTableDialog(vcl::Window* pParent,
        const uno::Reference< sdbc::XConnection>& rConnection)
    : SfxModalDialog(pParent, "SelectTableDialog", "modules/swriter/ui/selecttabledialog.ui")
    , m_xConnection(rConnection)
{
    get(m_pPreviewPB, "preview");

    SvSimpleTableContainer *pHeaderTreeContainer = get<SvSimpleTableContainer>("table");
    Size aSize = pHeaderTreeContainer->LogicToPixel(Size(238 , 50), MapMode(MapUnit::MapAppFont));
    pHeaderTreeContainer->set_width_request(aSize.Width());
    pHeaderTreeContainer->set_height_request(aSize.Height());
    m_pTable = VclPtr<SwAddressTable>::Create(*pHeaderTreeContainer);
    long const aStaticTabs[]= { 0, 0 };
    m_pTable->SetTabs( SAL_N_ELEMENTS(aStaticTabs), aStaticTabs );
    m_pTable->InsertHeaderItem(1, SwResId(ST_NAME) );
    m_pTable->InsertHeaderItem(2, SwResId(ST_TYPE) );

    m_pPreviewPB->SetClickHdl(LINK(this, SwSelectDBTableDialog, PreviewHdl));

    Reference<XTablesSupplier> xTSupplier(m_xConnection, UNO_QUERY);
    if (xTSupplier.is())
    {
        Reference<XNameAccess> xTables = xTSupplier->getTables();
        Sequence<OUString> aTables = xTables->getElementNames();
        const OUString* pTables = aTables.getConstArray();
        for(long i = 0; i < aTables.getLength(); i++)
        {
            OUString sEntry = pTables[i];
            sEntry += "\t";
            sEntry += SwResId(ST_TABLE);
            SvTreeListEntry* pEntry = m_pTable->InsertEntry(sEntry);
            pEntry->SetUserData(nullptr);
        }
    }
    Reference<XQueriesSupplier> xQSupplier(m_xConnection, UNO_QUERY);
    if (xQSupplier.is())
    {
        Reference<XNameAccess> xQueries = xQSupplier->getQueries();
        Sequence<OUString> aQueries = xQueries->getElementNames();
        const OUString* pQueries = aQueries.getConstArray();
        for(long i = 0; i < aQueries.getLength(); i++)
        {
            OUString sEntry = pQueries[i];
            sEntry += "\t";
            sEntry += SwResId(ST_QUERY);
            SvTreeListEntry* pEntry = m_pTable->InsertEntry(sEntry);
            pEntry->SetUserData(reinterpret_cast<void*>(1));
        }
    }
}

SwSelectDBTableDialog::~SwSelectDBTableDialog()
{
    disposeOnce();
}

void SwSelectDBTableDialog::dispose()
{
    m_pTable.disposeAndClear();
    m_pPreviewPB.clear();
    SfxModalDialog::dispose();
}

IMPL_LINK(SwSelectDBTableDialog, PreviewHdl, Button*, pButton, void)
{
    SvTreeListEntry* pEntry = m_pTable->FirstSelected();
    if(!pEntry)
        return;

    OUString sTableOrQuery = SvTabListBox::GetEntryText(pEntry, 0);
    sal_Int32 nCommandType = nullptr == pEntry->GetUserData() ? 0 : 1;

    OUString sDataSourceName;
    Reference<XChild> xChild(m_xConnection, UNO_QUERY);
    if(xChild.is())
    {
        Reference<XDataSource> xSource(xChild->getParent(), UNO_QUERY);
        Reference<XPropertySet> xPrSet(xSource, UNO_QUERY);
        xPrSet->getPropertyValue("Name") >>= sDataSourceName;
    }
    OSL_ENSURE(!sDataSourceName.isEmpty(), "no data source found");
    Sequence<PropertyValue> aProperties(5);
    PropertyValue* pProperties = aProperties.getArray();
    pProperties[0].Name = "DataSourceName";
    pProperties[0].Value <<= sDataSourceName;
    pProperties[1].Name = "Command";
    pProperties[1].Value <<= sTableOrQuery;
    pProperties[2].Name = "CommandType";
    pProperties[2].Value <<= nCommandType;
    pProperties[3].Name = "ShowTreeView";
    pProperties[3].Value <<= false;
    pProperties[4].Name = "ShowTreeViewButton";
    pProperties[4].Value <<= false;

    VclPtrInstance< SwDBTablePreviewDialog > pDlg(pButton, aProperties);
    pDlg->Execute();

}

OUString    SwSelectDBTableDialog::GetSelectedTable(bool& bIsTable)
{
    SvTreeListEntry* pEntry = m_pTable->FirstSelected();
    bIsTable = pEntry->GetUserData() == nullptr;
    return SvTabListBox::GetEntryText(pEntry, 0);
}

void   SwSelectDBTableDialog::SetSelectedTable(const OUString& rTable, bool bIsTable)
{
    SvTreeListEntry*    pEntry = m_pTable->First();
    while(pEntry)
    {
        if((SvTabListBox::GetEntryText(pEntry, 0) == rTable) &&
           ((pEntry->GetUserData() == nullptr ) == bIsTable))
        {
            m_pTable->Select(pEntry);
            break;
        }
        pEntry = m_pTable->Next( pEntry );
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
