/* $OpenBSD$ */

/*
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <stdlib.h>
#include <string.h>

#include "tmux.h"

/*
 * Display panes on a client.
 */

static enum args_parse_type	cmd_display_panes_args_parse(struct args *,
				    u_int, char **);
static enum cmd_retval		cmd_display_panes_exec(struct cmd *,
				    struct cmdq_item *);

const struct cmd_entry cmd_display_panes_entry = {
	.name = "display-panes",
	.alias = "displayp",

	.args = { "bd:Nt:", 0, 1, cmd_display_panes_args_parse },
	.usage = "[-bN] [-d duration] " CMD_TARGET_CLIENT_USAGE " [template]",

	.flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG,
	.exec = cmd_display_panes_exec
};

struct cmd_display_panes_data {
	struct cmdq_item		*item;
	struct args_command_state	*state;
};

struct cmd_display_panes_ctx {
	struct client	*c;
	int		 ox;
	int		 oy;
	u_int		 sx;
	u_int		 sy;
	u_int		 statuslines;
	int		 statustop;
};

static enum args_parse_type
cmd_display_panes_args_parse(__unused struct args *args, __unused u_int idx,
    __unused char **cause)
{
	return (ARGS_PARSE_COMMANDS_OR_STRING);
}

static void
cmd_display_panes_put(struct cmd_display_panes_ctx *ctx,
    struct window_pane *wp, u_int cx, u_int cy, const char *buf, size_t len)
{
	struct client		*c = ctx->c;
	struct tty		*tty = &c->tty;
	struct visible_ranges	*r;
	struct visible_range	*ri;
	u_int			 i, j;

	r = window_visible_ranges(wp, ctx->ox + cx, ctx->oy + cy, len, NULL);
	for (i = 0; i < r->used; i++) {
		ri = &r->ranges[i];
		for (j = ri->px; j < ri->px + ri->nx; j++) {
			tty_cursor(tty, j - ctx->ox, cy);
			tty_putn(tty, &buf[j - ri->px], 1, 1);
		}
	}
}

static void
cmd_display_panes_draw_format(struct cmd_display_panes_ctx *ctx,
    struct window_pane *wp, u_int xoff, u_int yoff, u_int sx,
    const struct grid_cell *gc)
{
	struct client		*c = ctx->c;
	struct tty		*tty = &c->tty;
	struct session		*s = c->session;
	struct screen		*sc = wp->screen, screen;
	struct screen_write_ctx	 sctx;
	struct visible_ranges	*r;
	struct visible_range	*ri;
	const char		*format;
	char			*expanded;
	u_int			 i, px = ctx->ox + xoff;

	format = options_get_string(s->options, "display-panes-format");
	expanded = format_single(NULL, format, c, s, s->curw, wp);

	screen_init(&screen, sx, 1, 0);
	screen_write_start(&sctx, &screen);
	screen_write_fast_copy(&sctx, sc, 0, sc->grid->hsize, sx, 1);
	screen_write_cursormove(&sctx, 0, 0, 0);
	format_draw(&sctx, gc, sx, expanded, NULL, 0);
	screen_write_stop(&sctx);
	free(expanded);

	r = window_visible_ranges(wp, px, wp->yoff, sx, NULL);
	for (i = 0; i < r->used; i++) {
		ri = &r->ranges[i];
		tty_draw_line(tty, &screen, ri->px - px, 0, ri->nx,
		    ri->px - ctx->ox, yoff, NULL);
	}
	screen_free(&screen);
}

static void
cmd_display_panes_draw_pane(struct cmd_display_panes_ctx *ctx,
    struct window_pane *wp)
{
	struct client		*c = ctx->c;
	struct tty		*tty = &c->tty;
	struct session		*s = c->session;
	struct options		*oo = s->options;
	struct window		*w = wp->window;
	struct grid_cell	 fgc, bgc;
	u_int			 pane, idx, px, py, i, j, xoff, yoff, sx, sy;
	u_int			 cx, cy;
	const char		*name;
	struct format_tree	*ft;
	char			 buf[16], lbuf[16], *ptr;
	size_t			 len, llen;

	if (wp->xoff + (int)wp->sx <= ctx->ox ||
	    wp->xoff >= ctx->ox + (int)ctx->sx ||
	    wp->yoff + (int)wp->sy <= ctx->oy ||
	    wp->yoff >= ctx->oy + (int)ctx->sy)
		return;

	if (wp->xoff >= ctx->ox && wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
		/* All visible. */
		xoff = wp->xoff - ctx->ox;
		sx = wp->sx;
	} else if (wp->xoff < ctx->ox &&
	    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
		/* Both left and right not visible. */
		xoff = 0;
		sx = ctx->sx;
	} else if (wp->xoff < ctx->ox) {
		/* Left not visible. */
		xoff = 0;
		sx = wp->sx - (ctx->ox - wp->xoff);
	} else {
		/* Right not visible. */
		xoff = wp->xoff - ctx->ox;
		sx = wp->sx - xoff;
	}
	if (wp->yoff >= ctx->oy && wp->yoff + wp->sy <= ctx->oy + ctx->sy) {
		/* All visible. */
		yoff = wp->yoff - ctx->oy;
		sy = wp->sy;
	} else if (wp->yoff < ctx->oy &&
	    wp->yoff + wp->sy > ctx->oy + ctx->sy) {
		/* Both top and bottom not visible. */
		yoff = 0;
		sy = ctx->sy;
	} else if (wp->yoff < ctx->oy) {
		/* Top not visible. */
		yoff = 0;
		sy = wp->sy - (ctx->oy - wp->yoff);
	} else {
		/* Bottom not visible. */
		yoff = wp->yoff - ctx->oy;
		sy = wp->sy - yoff;
	}

	if (ctx->statustop)
		yoff += ctx->statuslines;
	px = sx / 2;
	py = sy / 2;

	if (window_pane_index(wp, &pane) != 0)
		fatalx("index not found");
	len = xsnprintf(buf, sizeof buf, "%u", pane);

	if (sx < len)
		return;
	if (w->active == wp)
		name = "display-panes-active-colour";
	else
		name = "display-panes-colour";
	ft = format_create_defaults(NULL, c, s, NULL, wp);
	style_apply(&fgc, oo, name, ft);
	format_free(ft);

	memcpy(&bgc, &grid_default_cell, sizeof bgc);
	bgc.bg = fgc.fg;

	if (pane > 9 && pane < 35)
		llen = xsnprintf(lbuf, sizeof lbuf, "%c", 'a' + (pane - 10));
	else
		llen = 0;

	if (sx < len * 6 || sy < 5) {
		tty_attributes(tty, &fgc, NULL);
		if (sx >= len + llen + 1) {
			len += llen + 1;
			cx = xoff + px - len / 2;
			cy = yoff + py;
			cmd_display_panes_put(ctx, wp, cx, cy, buf, len);
			cx += len;
			cmd_display_panes_put(ctx, wp, cx, cy, " ", 1);
			cx++;
			cmd_display_panes_put(ctx, wp, cx, cy, lbuf, llen);
		} else {
			cx = xoff + px - len / 2;
			cy = yoff + py;
			cmd_display_panes_put(ctx, wp, cx, cy, buf, len);
		}
		goto out;
	}

	px -= len * 3;
	py -= 2;

	tty_attributes(tty, &bgc, NULL);
	for (ptr = buf; *ptr != '\0'; ptr++) {
		if (*ptr < '0' || *ptr > '9')
			continue;
		idx = *ptr - '0';

		for (j = 0; j < 5; j++) {
			for (i = px; i < px + 5; i++) {
				if (!window_clock_table[idx][j][i - px])
					continue;
				cx = xoff + i;
				cy = yoff + py + j;
				cmd_display_panes_put(ctx, wp, cx, cy, " ", 1);
			}
		}
		px += 6;
	}

	if (sy <= 6)
		goto out;
	cmd_display_panes_draw_format(ctx, wp, xoff, yoff, sx, &fgc);
	if (llen != 0) {
		tty_attributes(tty, &fgc, NULL);
		cx = xoff + sx / 2 + len * 3 - llen - 1;
		cy = yoff + py + 5;
		cmd_display_panes_put(ctx, wp, cx, cy, lbuf, llen);
	}

out:
	tty_cursor(tty, 0, 0);
}

static void
cmd_display_panes_draw(struct client *c, __unused void *data)
{
	struct session			*s = c->session;
	struct window			*w = s->curw->window;
	struct window_pane		*wp;
	struct cmd_display_panes_ctx	 ctx;
	u_int				 lines;

	log_debug("%s: %s @%u", __func__, c->name, w->id);

	memset(&ctx, 0, sizeof ctx);
	ctx.c = c;
	tty_window_offset(&c->tty, &ctx.ox, &ctx.oy, &ctx.sx, &ctx.sy);
	if (options_get_number(s->options, "status-position") == 0) {
		lines = status_line_size(c);
		if (c->message_string != NULL || c->prompt != NULL)
			lines = (lines == 0 ? 1 : lines);
		ctx.statuslines = lines;
		ctx.statustop = 1;
	}

	TAILQ_FOREACH(wp, &w->panes, entry) {
		if (window_pane_is_visible(wp))
			cmd_display_panes_draw_pane(&ctx, wp);
	}
}

static void
cmd_display_panes_free(__unused struct client *c, void *data)
{
	struct cmd_display_panes_data	*cdata = data;

	if (cdata->item != NULL)
		cmdq_continue(cdata->item);
	args_make_commands_free(cdata->state);
	free(cdata);
}

static int
cmd_display_panes_key(struct client *c, void *data, struct key_event *event)
{
	struct cmd_display_panes_data	*cdata = data;
	char				*expanded, *error;
	struct cmdq_item		*item = cdata->item, *new_item;
	struct cmd_list			*cmdlist;
	struct window			*w = c->session->curw->window;
	struct window_pane		*wp;
	u_int				 index;
	key_code			 key;

	if (event->key >= '0' && event->key <= '9')
		index = event->key - '0';
	else if ((event->key & KEYC_MASK_MODIFIERS) == 0) {
		key = (event->key & KEYC_MASK_KEY);
		if (key >= 'a' && key <= 'z')
			index = 10 + (key - 'a');
		else
			return (-1);
	} else
		return (-1);

	wp = window_pane_at_index(w, index);
	if (wp == NULL)
		return (1);
	window_unzoom(w, 1);

	xasprintf(&expanded, "%%%u", wp->id);

	cmdlist = args_make_commands(cdata->state, 1, &expanded, &error);
	if (cmdlist == NULL) {
		cmdq_append(c, cmdq_get_error(error));
		free(error);
	} else if (item == NULL) {
		new_item = cmdq_get_command(cmdlist, NULL);
		cmdq_append(c, new_item);
	} else {
		new_item = cmdq_get_command(cmdlist, cmdq_get_state(item));
		cmdq_insert_after(item, new_item);
	}

	free(expanded);
	return (1);
}

static enum cmd_retval
cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
{
	struct args			*args = cmd_get_args(self);
	struct client			*tc = cmdq_get_target_client(item);
	struct session			*s = tc->session;
	u_int				 delay;
	char				*cause;
	struct cmd_display_panes_data	*cdata;
	int				 wait = !args_has(args, 'b');

	if (tc->overlay_draw != NULL)
		return (CMD_RETURN_NORMAL);

	if (args_has(args, 'd')) {
		delay = args_strtonum(args, 'd', 0, UINT_MAX, &cause);
		if (cause != NULL) {
			cmdq_error(item, "delay %s", cause);
			free(cause);
			return (CMD_RETURN_ERROR);
		}
	} else
		delay = options_get_number(s->options, "display-panes-time");

	cdata = xcalloc(1, sizeof *cdata);
	if (wait)
		cdata->item = item;
	cdata->state = args_make_commands_prepare(self, item, 0,
	    "select-pane -t \"%%%\"", wait, 0);

	if (args_has(args, 'N')) {
		server_client_set_overlay(tc, delay, NULL, NULL,
		    cmd_display_panes_draw, NULL, cmd_display_panes_free, NULL,
		    cdata);
	} else {
		server_client_set_overlay(tc, delay, NULL, NULL,
		    cmd_display_panes_draw, cmd_display_panes_key,
		    cmd_display_panes_free, NULL, cdata);
	}

	if (!wait)
		return (CMD_RETURN_NORMAL);
	return (CMD_RETURN_WAIT);
}
