/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.htmls; import gplx.*; import gplx.xowa.*;
import gplx.xowa.htmls.portal.*;
public class Xoh_page_mgr implements Gfo_invk {
	private boolean font_enabled = false;
	private String font_name = "Arial";
	private byte[] font_css_bry = Bry_.Empty, custom_script = Bry_.Empty;
	private final    Bry_fmt font_css_fmt = Bry_fmt.Auto("body {font-family: ~{font_name}; font-size: ~{font_size}px;}");
	public float Font_size() {return font_size;} private float font_size = Font_size_default;
	private void Font_css_bry_() {
		font_css_bry = font_css_fmt.Bld_many_to_bry(Bry_bfr_.New(), font_name, font_size);
	}
	public void Write_css(gplx.xowa.htmls.heads.Xoh_head_wtr wtr) {
		if (font_enabled)
			wtr.Write_css_style_itm(font_css_bry);
		if (Bry_.Len_gt_0(custom_script))
			wtr.Write_css_style_itm(custom_script);
	}

	public Bry_fmt Content_code_fmt() {return content_code_fmt;} private final    Bry_fmt content_code_fmt = Bry_fmt.Auto("<pre>~{page_text}</pre>");
	public Xoh_subpages_bldr Subpages_bldr() {return subpages_bldr;} private final    Xoh_subpages_bldr subpages_bldr = new Xoh_subpages_bldr();
	public void Init_by_app(Xoa_app app) {
		app.Cfg().Bind_many_app(this, Cfg__font_enabled, Cfg__font_name, Cfg__font_size, Cfg__font_format, Cfg__custom_script);
	}
	public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
		if		(ctx.Match(k, Invk_content_code_fmt))		return String_.new_u8(content_code_fmt.Fmt());
		else if	(ctx.Match(k, Invk_content_code_fmt_))		content_code_fmt.Fmt_(m.ReadBry("v"));

		else if	(ctx.Match(k, Cfg__font_enabled))			font_enabled = m.ReadYn("v");
		else if	(ctx.Match(k, Cfg__font_name))				{font_name = m.ReadStr("v"); Font_css_bry_();}
		else if	(ctx.Match(k, Cfg__font_size))				{font_size = m.ReadFloat("v"); Font_css_bry_();}
		else if	(ctx.Match(k, Cfg__font_format))			{font_css_fmt.Fmt_(m.ReadBry("v")); Font_css_bry_();}
		else if	(ctx.Match(k, Cfg__custom_script))			custom_script = m.ReadBry("v");
		else	return Gfo_invk_.Rv_unhandled;
		return this;
	}
	private static final String Invk_content_code_fmt = "content_code_fmt", Invk_content_code_fmt_ = "content_code_fmt_";

	public static final String
	  Cfg__font_enabled			= "xowa.html.css.font.enabled"
	, Cfg__font_size			= "xowa.html.css.font.size"
	;
	private static final String
	  Cfg__font_name			= "xowa.html.css.font.name"
	, Cfg__font_format			= "xowa.html.css.font.format"
	, Cfg__custom_script		= "xowa.html.css.custom.script"
	;
	public static final float Font_size_default = 16;
}
