#include "General.h"

BOOLEAN IntDataTypeIsBigger(Data_Type dt1, Data_Type dt2)
{
    return (dt1.int_ >= dt2.int_);
}

void RemoveCDLLL(CDLLL * manager)
{
    while (manager->next != manager)
    {
        DeleteCDLLL(manager);
    }
    DeleteLastCDLLL(&manager);
}

void MergeCDLLL(CDLLL * manager1, CDLLL * manager2, BOOLEAN IsBigger(Data_Type dt1, Data_Type dt2))
{
    manager2->next->prev = manager1;
    manager1->next->prev = manager2;
    manager2->next = manager1;
    manager1->next = manager2;

    while (IsBigger(manager1->info,manager1->prev->info))
    {
        if (IsBigger(manager1->info, manager2->info))
        {
            InsertBeforeCDLLL(manager1);
            manager1->prev->info = manager2->info;
            manager2 = manager2->next;
        }
        else
        {
            manager1 = manager1->next;
        }
    }
    RemoveCDLLL(manager2);
}

unsigned int AmountNodesCDLLL(CDLLL * manager)
{
    unsigned int count = ONE;
    CDLLL * temp = manager->next;
    while (temp != manager)
    {
        count++;
        temp = temp->next;
    }
    
    return (count);
}

void CutCDLLLTwoParts(CDLLL * manager, CDLLL ** second_part)
{
    *second_part = manager;
    CDLLL * temp = manager;
    unsigned int counter;
    unsigned int amount_of_nodes = AmountNodesCDLLL(manager) / TWO;
    for (counter = ZERO; counter < amount_of_nodes; counter++)
    {
        *second_part = (*second_part)->next;
    }


}

void MergeSortCDLLL(CDLLL * manager)
{
    CDLLL * half_of_cdlll;
    if (manager->next != manager)
    {
        // cut the CDLLL for two parts <BLACKBOX>
        MergeSortCDLLL(manager);
        MergeSortCDLLL(half_of_cdlll);
        MergeCDLLL(manager, half_of_cdlll, IntDataTypeIsBigger);
    }
}

void main(void)
{
    CDLLL * manager1;
    InsertLastCDLLL(&manager1);
    InsertEndCDLLL(&manager1);
    InsertEndCDLLL(&manager1);
    InsertEndCDLLL(&manager1);
    InsertEndCDLLL(&manager1);
    InsertEndCDLLL(&manager1);
    InsertEndCDLLL(&manager1);
    InsertEndCDLLL(&manager1);

    CDLLL * manager2;
    InsertLastCDLLL(&manager2);
    InsertEndCDLLL(&manager2);
    InsertEndCDLLL(&manager2);
    InsertEndCDLLL(&manager2);
    InsertEndCDLLL(&manager2);
    InsertEndCDLLL(&manager2);
    InsertEndCDLLL(&manager2);
    InsertEndCDLLL(&manager2);


    int * seed;
    CDLLL * temp = manager1->next;
    
    PrintCDLLL(manager1, PrintIntDataType);
}