//////////////////////////////////////////////////////////////////////////////
//
// License Agreement:
//
// The following are Copyright © 2007, Casey Langen
//
// Sources and Binaries of: mC2, win32cpp
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met:
//
//    * Redistributions of source code must retain the above copyright notice,
//      this list of conditions and the following disclaimer.
//
//    * Redistributions in binary form must reproduce the above copyright 
//      notice, this list of conditions and the following disclaimer in the 
//      documentation and/or other materials provided with the distribution.
//
//    * Neither the name of the author nor the names of other contributors may 
//      be used to endorse or promote products derived from this software 
//      without specific prior written permission. 
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE. 
//
//////////////////////////////////////////////////////////////////////////////

#pragma once

//////////////////////////////////////////////////////////////////////////////

#include "pch.hpp"
#include <cube/SourcesModel.hpp>
#include <cube/TracklistView.hpp>
#include <cube/TracklistController.hpp>

//#include <core/tracklist/Playlist.h>

#include <win32cpp/Label.hpp>

using namespace musik::cube;

//////////////////////////////////////////////////////////////////////////////

class DummyItem: public SourcesItem, public EventHandler
{
private: /*ctor*/ DummyItem(const uistring& caption)
        : caption(caption)
        , view(uistring(caption + _T(" sources item")).c_str())
        {
            view.Created.connect(this, &DummyItem::OnViewCreated);
        }

public: void OnViewCreated(Window* window)
        {
            //this->view.SetBackgroundColor(Color(255, 255, 255));
        }

public: static SourcesItemRef Create(const uistring& caption)
        {
            return SourcesItemRef(new DummyItem(caption));
        }

public: virtual uistring Caption() { return this->caption; }
public: virtual Window*  View() { return &this->view; }

private: uistring caption;
private: Label view;
};

//////////////////////////////////////////////////////////////////////////////

#include <cube/BrowseView.hpp>
#include <cube/BrowseController.hpp>

class BrowseItem: public SourcesItem
{
private:    /*ctor*/ BrowseItem(musik::core::LibraryPtr library)
            : controller(view,library)
            {

            }

public:     /*dtor*/ ~BrowseItem()
            {
            }

public:     static SourcesItemRef Create(musik::core::LibraryPtr library)
            {
                return SourcesItemRef(new BrowseItem(library));
            }

public:     virtual uistring Caption() { return _(_T("Browse")); }
public:     virtual Window*  View()
            {
                return &this->view;
            }

private:    BrowseView view;
private:    BrowseController controller;
};

//////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////

class PlaylistItem: public SourcesItem
{
private:    

			/*ctor*/ PlaylistItem(musik::core::tracklist::Ptr playlist)
				: controller(view,NULL,playlist,TracklistController::HighlightActive|TracklistController::Deletable)
            {
            }

public:     /*dtor*/ ~PlaylistItem()
            {
            }

public:     static SourcesItemRef Create(musik::core::tracklist::Ptr playlist)
            {
                return SourcesItemRef(new PlaylistItem(playlist));
            }

public:     virtual uistring Caption() { 
//				musik::core::tracklist::Playlist* playlist = (musik::core::tracklist::Playlist*)(this->controller.Tracklist().get());
//				return playlist->Name(); 
                return uistring();
			}
public:     virtual Window*  View()
            {
                return &this->view;
            }

private:    TracklistView view;
private:    TracklistController controller;
};

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include <cube/SettingsView.hpp>
#include <cube/SettingsController.hpp>

class SettingsItem: public SourcesItem
{
private:    /*ctor*/ SettingsItem(musik::core::LibraryPtr library)
                : controller(view,library)
            {
            }

public:     /*dtor*/ ~SettingsItem()
            {
            }

public:     static SourcesItemRef Create(musik::core::LibraryPtr library)
            {
                return SourcesItemRef(new SettingsItem(library));
            }

public:     virtual uistring Caption() { return _(_T("Settings")); }
public:     virtual Window*  View()
            {
                return &this->view;
            }

private:    SettingsView view;
private:    SettingsController controller;
};

//////////////////////////////////////////////////////////////////////////////

typedef SourcesListModel::Category Category;
typedef SourcesListModel::CategoryRef CategoryRef;

/*ctor*/        SourcesModel::SourcesModel(musik::core::LibraryPtr library)
: library(library)
{
}

void SourcesModel::Load()
{
    CategoryRef viewCategory(new Category(_(_T("View"))));
    viewCategory->Add(BrowseItem::Create(this->library));
    if(this->library->Indexer()!=NULL){
        viewCategory->Add(SettingsItem::Create(this->library));
    }
    this->AddCategory(viewCategory);

    this->playlistCategory.reset(new Category(_(_T("Playlists"))));
/*    playlistCategory->Add(DummyItem::Create(_(_T("Playlist 1"))));
    playlistCategory->Add(DummyItem::Create(_(_T("Playlist 2"))));
    playlistCategory->Add(DummyItem::Create(_(_T("Dynamic Playlist 3"))));
    playlistCategory->Add(DummyItem::Create(_(_T("Dynamic Playlist 4"))));*/
    this->AddCategory(this->playlistCategory);

}

#define FIND_CATEGORY(list, category) std::find(list.begin(), list.end(), category)

void            SourcesModel::AddCategory(CategoryRef category)
{
    if (FIND_CATEGORY(this->categories, category) != this->categories.end())
    {
        throw InvalidCategoryException();
    }

    this->categories.push_back(category);
    this->CategoryAdded(category);
}

void            SourcesModel::RemoveCategory(CategoryRef category)
{
    CategoryList::iterator it = FIND_CATEGORY(this->categories, category);
    if (it == this->categories.end())
    {
        throw InvalidCategoryException();
    }

    this->categories.erase(it);
    this->CategoryRemoved(category);
}

/*void SourcesModel::OnPlaylists(SourcesModel::PlaylistVector &playlists){
	// Start by removing the old ones
	for(PlaylistItemMap::iterator it=this->playlistItemsMap.begin();it!=this->playlistItemsMap.end();++it){
		// Remove the item from the category
		this->playlistCategory->Remove(it->second);
	}

	// Clear the map
	this->playlistItemsMap.clear();

	// Start creating the new playlists
	for(SourcesModel::PlaylistVector::iterator playlist=playlists.begin();playlist!=playlists.end();++playlist){
		// Create the sourcesItem
		SourcesItemRef sourceItem	= PlaylistItem::Create(*playlist);
	    playlistCategory->Add(sourceItem);

		this->playlistItemsMap[*playlist]	= sourceItem;

	}

}
*/
