/*
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.wikis.tdbs.metas; import gplx.*; import gplx.xowa.*; import gplx.xowa.wikis.*; import gplx.xowa.wikis.tdbs.*;
import gplx.core.flds.*;
public class Xof_meta_fil {
	private final    Ordered_hash itms = Ordered_hash_.New_bry();
	public Xof_meta_fil(Xof_meta_mgr meta_mgr, byte[] md5) {this.meta_mgr = meta_mgr; this.md5 = md5;}
	public Xof_meta_mgr Owner_mgr() {return meta_mgr;} private final    Xof_meta_mgr meta_mgr;
	public byte[] Md5() {return md5;} private final    byte[] md5;
	public void Dirty_() {meta_mgr.Dirty_(this);}
	public Xof_meta_itm Get_or_new(byte[] ttl) {
		Xof_meta_itm rv = Get_or_null(ttl);
		if (rv == null) {
			rv = new Xof_meta_itm(this, ttl);
			itms.Add(ttl, rv);
		}
		return rv;
	}
	public Xof_meta_itm Get_or_null(byte[] ttl) {return (Xof_meta_itm)itms.Get_by(ttl);}
	public void Save(Gfo_fld_wtr wtr) {
		int itms_len = itms.Count();
		for (int i = 0; i < itms_len; i++) {
			Xof_meta_itm itm = (Xof_meta_itm)itms.Get_at(i);
			itm.Save(wtr);
		}
	}
	public Xof_meta_fil Load(Gfo_fld_rdr rdr, Xof_meta_thumb_parser parser) {
		itms.Clear();
		int bry_len = rdr.Data().length;
		while (rdr.Pos() < bry_len) {
			Xof_meta_itm itm = new Xof_meta_itm(this, Bry_.Empty);
			itm.Load(rdr, parser);
			itms.Add(itm.Ttl(), itm);
		}
		return this;
	}
	public static Io_url Bld_url(Io_url root, byte[] md5, int depth) {
		Bld_url_bfr.Add(root.RawBry());
		for (int i = 0; i < depth - 1; i++)
			Bld_url_bfr.Add_byte(md5[i]).Add_byte(root.Info().DirSpr_byte());
		for (int i = 0; i < depth; i++)
			Bld_url_bfr.Add_byte(md5[i]);
		Bld_url_bfr.Add(Bry_url_ext);
		return Io_url_.new_fil_(Bld_url_bfr.To_str_and_clear());
	}	private static final    byte[] Bry_url_ext = Bry_.new_a7(".csv"); static Bry_bfr Bld_url_bfr = Bry_bfr_.New_w_size(260);	// 260 is max path of url
}
