/* Copyright 2008 Jeffrey Lee
   This file is part of Pale.
   Pale is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   Pale is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with Pale.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "loadsave.h"
#include "sprfile.h"
#include "preview.h"
#include "editor.h"

#include "/WoumInclude:lib/futil.h"
#include "/WoumInclude:lib/osspr.h"

//DEPPEND

void save_file(char *name,sprfile *file)
{
	if((!name) || (!file))
		return;
	if(name != file->name)
	{
		free(file->name);
		file->name = malloc(strlen(name)+1);
		strcpy(file->name,name);
	}
	if(OSSpr_SaveFile(file->sprs,name))
	{
		report_error("Error saving file. Also, we need a more descriptive error message.");
	}
	else
		file->modified = 0;
	sprfile_updatetitle(file);
}

void save_palette(char *name,rgb *pal)
{
	FILE *f;
	int i;
	if((!name) || (!pal))
		return;
	f = fopen(name,"wb");
	if(!f)
	{
		report_error("Unable to open file for writing");
		return;
	}
	for(i=0;i<256;i++)
	{
		/* Just do basic !Paint-compatible palette for now */
		fputc(19,f);
		fputc(i,f);
		fputc(16,f);
		fputc(pal[i*2].r,f);
		fputc(pal[i*2].g,f);
		fputc(pal[i*2].b,f);
	}
	fclose(f);
	regs.r[0] = 18;
	regs.r[1] = (int) name;
	regs.r[2] = 0xfed;
	_kernel_swi(OS_File,&regs,&regs);
}

void save_sprite(char *name,preview *spr)
{
	if((!name) || (!spr))
		return;
	/* Concoct own spritearea
	   This doesn't save the correct palette, though.  */
	FILE *f = fopen(name,"wb");
	if(!f)
	{
		report_error("Unable to open file for writing");
		return;
	}
	futil_putword(f,1);
	futil_putword(f,16);
	futil_putword(f,*((int *) spr->spr)+16);
	fwrite(spr->spr,*((int *) spr->spr),1,f);
	fclose(f);
	regs.r[0] = 18;
	regs.r[1] = (int) name;
	regs.r[2] = 0xff9;
	_kernel_swi(OS_File,&regs,&regs);
}

static int parse_palette(rgb *dest,FILE *src)
{
	int i,c;
	if((!dest) || (!src))
		return 0;
	while(!feof(src))
	{
		c = fgetc(src);
		if(c == EOF)
			return 0;
		if(c != 19)
			return 1;
		i = fgetc(src);
		if(i == EOF)
			return 1;
		c = fgetc(src);
		if(c == 16)
		{
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2].r = c;
			dest[i*2+1].r = c;
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2].g = c;
			dest[i*2+1].g = c;
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2].b = c;
			dest[i*2+1].b = c;
		}
		else if(c == 17)
		{
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2].r = c;
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2].g = c;
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2].b = c;
		}
		else if(c == 18)
		{
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2+1].r = c;
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2+1].g = c;
			c = fgetc(src);
			if(c == EOF)
				return 1;
			dest[i*2+1].b = c;
		}
		else
			return 1;
	}
	return 0;
}

void load_palette1(sprfile *s,int sprite,char *name)
{
	if((!s) || (sprite < 0) || (sprite >= OSSpr_CountSprites(s->sprs)) || (!name))
		return;
	void *spr = sprfile_getsprite(s,sprite);
	rgb *pal = sprfile_getpalette(spr);
	if(!pal)
		return;
	FILE *f = fopen(name,"rb");
	if(!f)
		return;
	if(parse_palette(pal,f))
		report_error("Corrupt palette file");
	fclose(f);
	sprfile_redraw(s,spr);
	if(!s->modified)
	{
		s->modified = 1;
		sprfile_updatetitle(s);
	}
}

void load_palette2(preview *p,char *name)
{
	if((!p) || (!name))
		return;
	rgb *pal = sprfile_getpalette(p->spr);
	if(!pal)
		return;
	FILE *f = fopen(name,"rb");
	if(!f)
		return;
	if(parse_palette(pal,f))
		report_error("Corrupt palette file");
	fclose(f);
	sprfile_redraw(p->file,p->spr);
	if(!p->file->modified)
	{
		p->file->modified = 1;
		sprfile_updatetitle(p->file);
	}
}

void load_palette3(paleditor *e,char *name)
{
	if((!e) || (!name))
		return;
	FILE *f = fopen(name,"rb");
	if(!f)
		return;
	if(parse_palette(e->pal,f))
	{
		report_error("Corrupt palette file");
		fclose(f);
		editor_redrawpal(e);
	}
	editor_redrawpal(e);
	/* apply to targets */
	int i;
	for(i=0;i<e->nsprites;i++)
	{
		fseek(f,0,SEEK_SET);
		parse_palette(sprfile_getpalette(e->sprites[i]),f);
	}
	fclose(f);
	/* redraw any open windows */
	preview *p = e->file->previews;
	while(p)
	{
		if(p->pal == e)
			preview_redrawall(p);
		p = p->next;
	}
	if(!e->file->modified)
	{
		e->file->modified = 1;
		sprfile_updatetitle(e->file);
	}
}

sprfile *load_spritefile(char *name)
{
	if(!name)
		return 0;
	void *spr = OSSpr_LoadFile(name);
	if(!spr)
		return 0;
	sprfile *file = malloc(sizeof(sprfile));
	file->name = malloc(strlen(name)+1);
	strcpy(file->name,name);
	file->sprs = spr;
	file->len = *((int *) spr);
	file->mainwin = LimpX_CreateWindow(temp_spritefile);
	file->editors = 0;
	file->previews = 0;
	file->modified = 0;
	file->next = sprfiles;
	file->prev = 0;
	if(sprfiles)
		sprfiles->prev = file;
	sprfiles = file;
	/* Build icons in main window */
	sprfile_rebuildicons(file);
	sprfile_updatetitle(file);
	LimpX_OpenWindow(file->mainwin);
	return file;
}

