/*
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.langs.numbers; import gplx.*; import gplx.xowa.*; import gplx.xowa.langs.*;
public class Xol_num_mgr implements Gfo_invk {
	private boolean digits_translate;
	protected Bry_bfr tmp_bfr = Bry_bfr_.Reset(32);
	private static final    byte[] Comma_bry = Bry_.new_a7(",");
	public Xol_num_grp_fmtr Num_grp_fmtr() {return num_grp_fmtr;} private Xol_num_grp_fmtr num_grp_fmtr = new Xol_num_grp_fmtr();
	public Xol_transform_mgr Separators_mgr() {return separators_mgr;} private Xol_transform_mgr separators_mgr = new Xol_transform_mgr();
	public Xol_transform_mgr Digits_mgr() {return digits_mgr;} private Xol_transform_mgr digits_mgr = new Xol_transform_mgr();		
	public byte[] Raw(byte[] num) {
		if (digits_translate)
			num = digits_mgr.Replace(tmp_bfr, num, false);
		num = separators_mgr.Replace(tmp_bfr, num, false);
		num = Bry_.Replace_safe(tmp_bfr, num, Comma_bry, Bry_.Empty);
		return num;
	}
	public byte[] Format_num_no_separators(byte[] num) {return Format_num(num, true);}
	public byte[] Format_num_by_long(long val)			{return Format_num(Bry_.new_a7(Long_.To_str(val)));}
	public byte[] Format_num_by_decimal(Decimal_adp val){return Format_num(Bry_.new_a7(val.To_str()));}
	public byte[] Format_num(int val)					{return Format_num(Bry_.new_a7(Int_.To_str(val)));}
	public byte[] Format_num(byte[] num)				{return Format_num(num, false);}
	public byte[] Format_num(byte[] num, boolean skip_commafy) {
		if (!skip_commafy) {
			num = Commafy(num);
			num = separators_mgr.Replace(tmp_bfr, num, true);
		}
		if (digits_translate)
			num = digits_mgr.Replace(tmp_bfr, num, true);
		return num;
	}
	@gplx.Virtual public byte[] Commafy(byte[] num_bry) {
		if (num_bry == null) return Bry_.Empty;	// MW: if ( $number === null ) return '';
		if (num_grp_fmtr.Mode_is_regx())
			return num_grp_fmtr.Fmt_regx(tmp_bfr, num_bry);
		else	// NOTE: for now, return same as ###,###,###; only affects 12 languages; current implementation is bad; https://bugzilla.wikimedia.org/show_bug.cgi?id=63977
			return num_grp_fmtr.Fmt_regx(tmp_bfr, num_bry);
	}
	public Xol_num_mgr Clear() {
		digits_mgr.Clear();
		separators_mgr.Clear();
		num_grp_fmtr.Clear();
		return this;
	}
	public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
		if		(ctx.Match(k, Invk_clear))						this.Clear();
		else if	(ctx.Match(k, Invk_separators))					return separators_mgr;
		else if	(ctx.Match(k, Invk_digits))						{digits_translate = true; return digits_mgr;}	// NOTE: only langes with a digit_transform_table will call digits; DATE:2014-05-28
		else if	(ctx.Match(k, Invk_digit_grouping_pattern))		return String_.new_u8(num_grp_fmtr.Digit_grouping_pattern());
		else if	(ctx.Match(k, Invk_digit_grouping_pattern_))	num_grp_fmtr.Digit_grouping_pattern_(m.ReadBry("v"));
		else	return Gfo_invk_.Rv_unhandled;
		return this;
	}
	public static final String Invk_clear = "clear", Invk_separators = "separators"
	, Invk_digits = "digits", Invk_digit_grouping_pattern = "digit_grouping_pattern", Invk_digit_grouping_pattern_ = "digit_grouping_pattern_";
	public static final    byte[]
	  Separators_key__grp = new byte[]{Byte_ascii.Comma}
	, Separators_key__dec = new byte[]{Byte_ascii.Dot}
	;
}
