#include <stdio.h>
#include <SDL.h>

#include "portab.h"
#include "system.h"
#include "cg.h"
#include "ags.h"
#include "night.h"
#include "sprite.h"
#include "nt_scenario.h"
#include "sactcg.h"

// 壁紙スプライト表示位置
#define WALL_LOC_X 0
#define WALL_LOC_Y 0
// 風景スプライト表示位置
#define SCENERY_LOC_X 0
#define SCENERY_LOC_Y 64
// 顔スプライト表示位置
#define FACE_LOC_X 6
#define FACE_LOC_Y 272
// 立ち絵左スプライト表示位置
#define TACHI_L_LOC_X 0
#define TACHI_L_LOC_Y 0
// 立ち絵中央スプライト位置
#define TACHI_M_LOC_X 160
#define TACHI_M_LOC_Y 0
// 立ち絵右スプライト位置
#define TACHI_R_LOC_X 360
#define TACHI_R_LOC_Y 0

static int effecttime;

void nt_gr_init() {
}

void nt_gr_set_wallpaper(int no) {
	sprite_t *sp = night.sp[SPNO_WALL];
	
	if (sp) {
		nt_sp_remove_updatelist(sp);
		nt_sp_free(sp);
	}
	
	if (no == 1013 && nt_sco_is_natsu()) {
		no = 1011;
	}
	
	if (no == 1014 && nt_sco_is_natsu()) {
		no = 1012;
	}
	
	sp = nt_sp_new(SPNO_WALL, no, 0, 0, SPRITE_WP);
	nt_sp_add_updatelist(sp);
	if (no == 0) {
		sp->width  = main_surface->w;
		sp->height = main_surface->h;
		sp->update = nt_sp_draw_wall;
	}
	
	night.sp[SPNO_WALL] = sp;
}

void nt_gr_set_scenery(int no) {
	sprite_t *sp = night.sp[SPNO_SCENERY];

	if (sp) {
		nt_sp_remove_updatelist(sp);
		nt_sp_free(sp);
		sp = NULL;
	}

	if (no) {
		sp = nt_sp_new(SPNO_SCENERY, no, 0, 0, SPRITE_NORMAL);
		nt_sp_add_updatelist(sp);
		nt_sp_set_loc(sp, SCENERY_LOC_X, SCENERY_LOC_Y);
	}
	
	night.sp[SPNO_SCENERY] = sp;
}

void nt_gr_set_face(int no) {
	sprite_t *sp = night.sp[SPNO_FACE];
	
	if (sp) {
		nt_sp_remove_updatelist(sp);
		nt_sp_free(sp);
		sp = NULL;
	}
	if (no) {
		sp = nt_sp_new(SPNO_FACE, no, 0, 0, SPRITE_NORMAL);
		nt_sp_add_updatelist(sp);
		nt_sp_set_loc(sp, FACE_LOC_X, FACE_LOC_Y);
		night.msgplace = 2;
	} else {
		night.msgplace = 0;
	}
	night.sp[SPNO_FACE] = sp;
}

void nt_gr_set_spL(int no) {
	sprite_t *sp = night.sp[SPNO_TACHI_L];
	
	if (sp) {
		nt_sp_remove_updatelist(sp);
		nt_sp_free(sp);
		sp = NULL;
	}
	if (no) {
		sp = nt_sp_new(SPNO_TACHI_L, no, 0, 0, SPRITE_NORMAL);
		nt_sp_add_updatelist(sp);
		nt_sp_set_loc(sp, TACHI_L_LOC_X, TACHI_L_LOC_Y);
	}
	night.sp[SPNO_TACHI_L] = sp;
}

void nt_gr_set_spM(int no) {
	sprite_t *sp = night.sp[SPNO_TACHI_M];
	
	if (sp) {
		nt_sp_remove_updatelist(sp);
		nt_sp_free(sp);
		sp = NULL;
	}
	if (no) {
		sp = nt_sp_new(SPNO_TACHI_M, no, 0, 0, SPRITE_NORMAL);
		nt_sp_add_updatelist(sp);
		nt_sp_set_loc(sp, TACHI_M_LOC_X, TACHI_M_LOC_Y);
	}
	night.sp[SPNO_TACHI_M] = sp;
}

void nt_gr_set_spR(int no) {
	sprite_t *sp = night.sp[SPNO_TACHI_R];

	if (sp) {
		nt_sp_remove_updatelist(sp);
		nt_sp_free(sp);
		sp = NULL;
	}
	if (no) {
		sp = nt_sp_new(SPNO_TACHI_R, no, 0, 0, SPRITE_NORMAL);
		nt_sp_add_updatelist(sp);
		nt_sp_set_loc(sp, TACHI_R_LOC_X, TACHI_R_LOC_Y);
	}
	night.sp[SPNO_TACHI_R] = sp;
}

void nt_gr_set_spsL(int no) {
	if (no) {
		if (nt_sco_is_natsu()) no++;
	}
	nt_gr_set_spL(no);
}

void nt_gr_set_spsM(int no) {
	if (no) {
		if (nt_sco_is_natsu()) no++;
	}
	nt_gr_set_spM(no);
}

void nt_gr_set_spsR(int no) {
	if (no) {
		if (nt_sco_is_natsu()) no++;
	}
	nt_gr_set_spR(no);
}

void nt_gr_set_drawtime(int msec) {
	effecttime = msec;
}

void nt_gr_draw(int effectno) {
	switch(effectno) {
	case 0:
		// 全消し
		break;
	case 1:
		nt_sp_update_all(true);
		break;
	default:
		if (night.waitskiplv > 1) {
			nt_sp_update_all(true);
			break;
		}
		//nt_sp_eupdate(effectno, effecttime, true);
		nt_sp_eupdate(effectno, 1000, true);
	}
}

void nt_gr_screencg(int no, int x, int y) {
	SDL_Surface *sf = cg_load_as_sdlsurface(no - 1);
	SDL_SetSurfaceBlendMode(sf, SDL_BLENDMODE_ADD);
	SDL_BlitSurface(sf, NULL, main_surface, &(SDL_Rect){x, y, sf->w, sf->h});
	ags_updateArea(x, y, sf->w, sf->h);
	SDL_FreeSurface(sf);
}
