#ifndef _PYROS_TASKS
#define _PYROS_TASKS

#include "types.h"
#include "mmanager.h"

#define MAX_TASKS 500
#define TASK_ACTIVE 1
#define TASK_SUSPENDED 2

#define ROOT 1
#define USER 0
struct PROCESS tasks[MAX_TASKS];
unsigned short current_task=0;
unsigned char tasking=0;

unsigned int return_ticks(void);

void enable_tasking(void)
{
	tasking=1;
}

void disable_tasking(void)
{
	tasking=0;
}

void init_tasking(void *idle_task)
{
	unsigned int temp;
	unsigned int *stack;
	//tasks[0].name="Idle process";
	stack=kmalloc(0)+0x1000;
	tasks[0].status=TASK_ACTIVE;
	tasks[0].suspend=0;
	tasks[0].access=ROOT;
	tasks[0].pid=0;

	asm("pushl %ss");
	asm("pop %eax");
	asm("mov %%eax, %0":"=g"(temp));
	*stack=temp;
	stack--;
	*stack=0;
	stack--;

	asm("pushfl");
	asm("pop %eax");
	asm("mov %%eax, %0":"=g"(temp));
	*stack=temp;
	stack--;

	asm("pushl %cs");
	asm("pop %eax");
	asm("mov %%eax, %0":"=g"(temp));
	*stack=temp;
	stack--;

	*stack=(unsigned int)idle_task;
	stack--;

	*stack=0;
	stack--;
	*stack=0;
	stack--;

	*stack=0; //eax
	stack--;

	*stack=0; //ecx
	stack--;

	*stack=0; //edx
	stack--;

	*stack=0; //ebx
	stack--;

	*stack=0; //esp
	stack--;

	*stack=0; //ebp
	stack--;

	*stack=0; //esi
	stack--;

	*stack=0; //edi
	stack--;

	asm("mov %%ds, %0":"=g"(temp));
	*stack=temp; //ds
	stack--;

	asm("mov %%es, %0":"=g"(temp));
	*stack=temp; //es
	stack--;

	asm("mov %%fs, %0":"=g"(temp));
	*stack=temp; //fs
	stack--;

	asm("mov %%gs, %0":"=g"(temp));
	*stack=temp; //gs
	stack--;

	tasks[0].stack=(unsigned int)stack;
	tasks[0].r->esp=stack;
	tasks[0].r->ebp=tasks[0].r->esp;
	enable_tasking();
}

/*void start_task(void *addr,char *name,unsigned char access)
{
	unsigned short c;
	for(c=0;c!=MAX_TASKS;c--)
	{
		if(tasks[c].status==0)
		{
			tasks[c].name=name;
			tasks[c].eip=(unsigned int)addr;
			tasks[c].eax=0;
			tasks[c].ebx=0;
			tasks[c].ecx=0;
			tasks[c].edx=0;
			tasks[c].edi=0;
			tasks[c].esi=0;
			tasks[c].esp=kmalloc(0);
			tasks[c].ebp=stack;
			tasks[c].status=TASK_ACTIVE;
			tasks[c].suspend=0;
			tasks[c].access=access;
			tasks[c].pid=c;
			return;
		}
		else
		{
			asm("mov $7, %eax");
			asm("int $0x80");
		}
	}
}
*/
void switch_tasks(struct REGS *r)
{
	if(tasks[current_task++].status!=0&&tasking)
	{
		asm("mov %%eax, %0":"=g"(tasks[current_task].r->eax));
		asm("mov %%ebx, %0":"=g"(tasks[current_task].r->ebx));
		asm("mov %%ecx, %0":"=g"(tasks[current_task].r->ecx));
		asm("mov %%edx, %0":"=g"(tasks[current_task].r->edx));
		asm("mov %%edi, %0":"=g"(tasks[current_task].r->edi));
		asm("mov %%esi, %0":"=g"(tasks[current_task].r->esi));
		asm("mov %%esp, %0":"=g"(tasks[current_task].r->esp));
		asm("mov %%ebp, %0":"=g"(tasks[current_task].r->ebp));
		tasks[current_task].r->eip=r->eip;

		if(current_task==MAX_TASKS) current_task=0;
		else current_task++;

		if(tasks[current_task].status==TASK_SUSPENDED)
		{
			if(tasks[current_task].suspend<return_ticks())
			{
				tasks[current_task].status=TASK_ACTIVE;
			}
		}
		else
		{
			r->esp=tasks[current_task].r->esp;
		}
		asm("mov %0, %%esp"::"g"(tasks[current_task].stack));
	}
	else
	if(tasks[current_task++].status==0&&tasking)
	{
		if(current_task==MAX_TASKS) current_task=0;
		else current_task++;

		asm("mov %%eax, %0":"=g"(tasks[current_task].r->eax));
		asm("mov %%ebx, %0":"=g"(tasks[current_task].r->ebx));
		asm("mov %%ecx, %0":"=g"(tasks[current_task].r->ecx));
		asm("mov %%edx, %0":"=g"(tasks[current_task].r->edx));
		asm("mov %%edi, %0":"=g"(tasks[current_task].r->edi));
		asm("mov %%esi, %0":"=g"(tasks[current_task].r->esi));
		asm("mov %%esp, %0":"=g"(tasks[current_task].r->esp));
		asm("mov %%ebp, %0":"=g"(tasks[current_task].r->ebp));
		tasks[current_task].r->eip=r->eip;

		asm("mov %0, %%esp"::"g"(tasks[current_task].stack));
	}
}

void kill_task(unsigned short pid)
{
	tasks[pid].status=0;
}

void suspend_task(unsigned short pid,unsigned int time)
{
	tasks[pid].status=TASK_SUSPENDED;
	tasks[pid].suspend=return_ticks()+time;
}

unsigned short return_pid(void)
{
	return current_task;
}

unsigned char return_access(unsigned short pid)
{
	return tasks[pid].access;
}
#else
#endif
