// sattool - visual satellite tracking and prediction tool.
// Copyright 2000 Tom Rothamel <tom-idbg@onegeek.org>
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  

#include "sattool.h"
#include "filter.h"

int norad(Thing *t, CompareStep *cs, char cmp) {
	Sat *s;

	s = getSat(t);
	if (!s) return 0;

	return comparei(s->norad, cs, cmp);
}


// A pass is lit if it's lit at the start or end of the pass.
// An encounter is lit if it's lit at the moment.
int lit(Thing *t, CompareStep *cs, char cmp) {
	Object *s;
	Pass *p;
	Position *pos;
	Encounter *e;
	int rv = 0;


	// Check if it's an encounter
	e = getEncounter(t);
	if (e) {
		return isLit(e->objp);
	}
			
	p = getPass(t);
	if (!p) return 0;

	s = p->sat;

	// Check if it's lit at the start
	pos = s->calcpos(p->min0, NULL);
	rv = isLit(pos);
	delete pos;

	if (rv) return rv;
	
	// Check if it's lit at the end
       	pos = s->calcpos(p->min1, NULL);
	rv = isLit(pos);
	delete pos;

	return rv;
}

int mag(Thing *t, CompareStep *cs, char cmp) {
	Encounter *e;
	Sat *s;

	e = getEncounter(e);
	if (!e) return 0;

	s = getSat(e);
	if (!s) return 0;

	if (!s->hasmag()) return 0;

	return compared(s->calcmag(e), cs, cmp);
}
	

int sunel(Thing *t, CompareStep *cs, char cmp) {
	Pass *p;
	double el;
	
	p = getPass(t);
	if (!p) return 0;

	el = SunElevation(p->site, p->max);
	return compared(el, cs, cmp);
}

int minmag(Thing *t, CompareStep *cs, char cmp) {
	Pass *p;
	
	p = getPass(t);
	if (!p) return 0;

	return compared(p->minmag, cs, cmp);
}

int alwaystrue(Thing *t, CompareStep *cs, char cmp) {
	return -1;
}

int alwaysfalse(Thing *t, CompareStep *cs, char cmp) {
	return 0;
}

compare_tab compare_func_table[] = {
	{"norad", norad},
	{"lit", lit},
	{"mag", mag},
	{"sunel", sunel},
	{"minmag", minmag},
	{"true", alwaystrue},
	{"false", alwaysfalse}
};
