/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2025 Tibor 'Igor2' Palinkas
 *
 *  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., 31 Milk Street, # 960789 Boston, MA 02196 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */

/* included from act_read.c */

#include <librnd/core/compat_misc.h>
#include "data.h"
#include "layer.h"
#include "layer_addr.h"


static rnd_rtree_t **pcb_rtree_lookup_by_name(pcb_data_t *data, rnd_layer_id_t lid, const char *rtree_name, int is_global)
{
	if (is_global) {
		switch(*rtree_name) {
			case 'p': if (strcmp(rtree_name, "padstack") == 0) return &data->padstack_tree; break;
			case 's': if (strcmp(rtree_name, "subc") == 0) return &data->subc_tree; break;
			case 'r': if (strcmp(rtree_name, "rat") == 0) return &data->rat_tree; break;
		}
	}
	else {
		pcb_layer_t *ly;
		if ((lid < 0) || (lid >= data->LayerN)) {
			rnd_message(RND_MSG_ERROR, "RtreeList(): invalid layer (case 1)\n");
			return NULL;
		}
		ly = &(data->Layer[lid]);
		if (ly == NULL) {
			rnd_message(RND_MSG_ERROR, "RtreeList(): invalid layer (case 2)\n");
			return NULL;
		}
		switch(*rtree_name) {
			case 'l': if (strcmp(rtree_name, "line") == 0) return &ly->line_tree; break;
			case 't': if (strcmp(rtree_name, "text") == 0) return &ly->text_tree; break;
			case 'p': if (strcmp(rtree_name, "polygon") == 0) return &ly->polygon_tree; break;
			case 'a': if (strcmp(rtree_name, "arc") == 0) return &ly->arc_tree; break;
			case 'g': if (strcmp(rtree_name, "gfx") == 0) return &ly->gfx_tree; break;
		}
	}

	return NULL;
}

static const char pcb_acts_RtreeList[] = "RtreeList(data_addr, layer_addr|global, rtree_name, [x1, y1, x2, y2])\n";
static const char pcb_acth_RtreeList[] = "Return a list of idpaths for objects overlapping with the box specified (empty list if no object is found). Error is indicated by returning nil. If coordinates are not specified the whole tree is searched.";
/* DOC: rtreelist.html */
static fgw_error_t pcb_act_RtreeList(fgw_arg_t *res, int argc, fgw_arg_t *argv)
{
	pcb_board_t *pcb = PCB_ACT_BOARD;
	pcb_data_t *data;
	rnd_rtree_t *rtree, **rtp;
	const char *lyaddr, *rtree_name;
	rnd_layer_id_t lid = -1;
	rnd_rtree_it_t it;
	pcb_any_obj_t *o;
	pcb_idpath_list_t *list;
	int has_bbox = 0, is_global = 0;
	rnd_rtree_box_t qbox;

	RND_ACT_CONVARG(1, FGW_DATA, RtreeList, data = fgw_data(&argv[1]));
	RND_ACT_CONVARG(2, FGW_STR, RtreeList, lyaddr = argv[2].val.str);

	if (strcmp(lyaddr, "global") == 0)
		is_global = 1;
	else
		lid = pcb_layer_str2id(data, lyaddr);

	RND_ACT_CONVARG(3, FGW_STR, RtreeList, rtree_name = argv[3].val.str);
	rtp = pcb_rtree_lookup_by_name(data, lid, rtree_name, is_global);
	if (rtp == NULL) {
		rnd_message(RND_MSG_ERROR, "RtreeList(): invalid rtree name '%s'\n", rtree_name);
		return FGW_ERR_ARG_CONV;
	}

	rtree = *rtp;
	if (rtree == NULL) {
		/* uninitialized rtree means no result */
		res->type = FGW_VOID | FGW_PTR;
		res->val.ptr_void = 0;
		return 0;
	}


	if (argc > 4) {
		RND_ACT_CONVARG(4, FGW_COORD, RtreeList, qbox.x1 = fgw_coord(&argv[4]));
		RND_ACT_CONVARG(5, FGW_COORD, RtreeList, qbox.y1 = fgw_coord(&argv[5]));
		RND_ACT_CONVARG(6, FGW_COORD, RtreeList, qbox.x2 = fgw_coord(&argv[6]));
		RND_ACT_CONVARG(7, FGW_COORD, RtreeList, qbox.y2 = fgw_coord(&argv[7]));
		has_bbox = 1;
	}

	list = calloc(sizeof(pcb_idpath_list_t), 1);
	fgw_ptr_reg(&rnd_fgw, res, RND_PTR_DOMAIN_IDPATH_LIST, FGW_PTR | FGW_STRUCT, list);

	if (has_bbox) {
		for(o = rnd_rtree_first(&it, rtree, &qbox); o != NULL; o = rnd_rtree_next(&it)) {
			fgw_arg_t tmp;
			pcb_idpath_t *idp = pcb_obj2idpath(o);
			fgw_ptr_reg(&rnd_fgw, &tmp, RND_PTR_DOMAIN_IDPATH, FGW_PTR | FGW_STRUCT, idp);
			pcb_idpath_list_append(list, idp);
		}
	}
	else {
		for(o = rnd_rtree_all_first(&it, rtree); o != NULL; o = rnd_rtree_all_next(&it)) {
			fgw_arg_t tmp;
			pcb_idpath_t *idp = pcb_obj2idpath(o);
			fgw_ptr_reg(&rnd_fgw, &tmp, RND_PTR_DOMAIN_IDPATH, FGW_PTR | FGW_STRUCT, idp);
			pcb_idpath_list_append(list, idp);
		}
	}

	return 0;
}

