/*
Storybook: Open Source software for novelists and authors.
Copyright (C) 2008 - 2012 Martin Mustun

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 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package storybook.model.hbn.entity;

import i18n.I18N;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Node;
import storybook.model.DB.DATA;
import storybook.model.book.Book;
import storybook.model.hbn.dao.DAOutil;
import storybook.tools.ListUtil;
import storybook.tools.xml.XmlKey.XK;
import storybook.tools.xml.XmlUtil;

/**
 * Part generated by hbm2java
 *
 * @hibernate.class table="CATEGORY"
 */
public class Category extends AbstractEntity {

	public static String listToString(List<Category> categories) {
		List<String> list = new ArrayList<>();
		if (categories == null) {
			return ("");
		}
		for (Category c : categories) {
			list.add(c.getName());
		}
		ListUtil.setUnique(list);
		return (ListUtil.join(list, ", "));
	}

	public enum TYPE {
		PERSON, EVENT, IDEA, ITEM, PLOT, TAG, PHOTO;

		@Override
		public String toString() {
			return (this.name().toLowerCase());
		}
	}

	public static String getTYPEString(int t) {
		for (TYPE tp : TYPE.values()) {
			if (tp.ordinal() == t) {
				return (I18N.getMsg(tp.toString()));
			}
		}
		return (I18N.getMsg(TYPE.PERSON.toString()));
	}

	public static TYPE getTYPE(int t) {
		for (TYPE tp : TYPE.values()) {
			if (tp.ordinal() == t) {
				return (tp);
			}
		}
		return (TYPE.PERSON);
	}

	public static List<String> getTYPEList() {
		List<String> list = new ArrayList<>();
		for (TYPE t : TYPE.values()) {
			list.add(I18N.getMsg(t.toString()));
		}
		return (list);
	}

	public static Integer getSortNext(List<Category> entities) {
		int n = 0;
		for (Category c : entities) {
			if (c.getSort() > n) {
				n = c.getSort();
			}
		}
		return (n + 1);
	}

	//private Integer type;
	private Integer sort = 0;
	private Category sup;

	public Category() {
		super(Book.TYPE.CATEGORY, "110");
	}

	public Category(ResultSet rs) {
		super(Book.TYPE.CATEGORY, "110", rs);
		try {
			sort = rs.getInt("sort");
			sup = rs.getObject("sup", Category.class);
		} catch (SQLException ex) {
			//empty
		}
	}

	public Category(Integer sort, String name, Category sup) {
		this();
		this.sort = sort;
		setName(name);
		this.sup = sup;
	}

	public Category(Long id, String name, Integer sort, Category sup) {
		this();
		setId(id);
		setName(name);
		this.sort = sort;
		this.sup = sup;
	}

	public Integer getSort() {
		return (sort == null ? 0 : sort);
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	/*public Integer getType() {
		return(type==null?0:this.type);
	}

	public String getTypeString() {
		TYPE t=getTYPE(getType());
		return(I18N.getMsg(t.toString()));
	}

	public void setType(TYPE type) {
		this.type=type.ordinal();
	}

	public void setType(Integer type) {
		this.type = type;
	}*/
	public boolean hasSup() {
		return sup != null;
	}

	public Category getSup() {
		return this.sup;
	}

	public void setSup(Category sup) {
		this.sup = sup;
	}

	@Override
	public String toCsv(String quoteStart, String quoteEnd, String separator) {
		StringBuilder b = new StringBuilder();
		b.append(quoteStart).append(getClean(this)).append(quoteEnd).append(separator);
		b.append(quoteStart).append(getClean(getName())).append(quoteEnd).append(separator);
		b.append(quoteStart).append(getClean(sort)).append(quoteEnd).append(separator);
		b.append(quoteStart).append(getClean(sup)).append(quoteEnd).append("\n");
		return (b.toString());
	}

	@Override
	public String toDetail(Integer detailed) {
		StringBuilder b = new StringBuilder();
		b.append(toDetailHeader(detailed));
		b.append(getInfo(detailed, DATA.SORT, getSort().toString()));
		b.append(getInfo(detailed, DATA.CATEGORY_SUP, getSup()));
		b.append(toDetailFooter(detailed));
		return (b.toString());
	}

	@Override
	public String toHtml() {
		return (toCsv("<td>", "</td>", "\n"));
	}

	@Override
	public String toText() {
		return (toCsv("", "", "\t"));
	}

	@Override
	public String toXml() {
		StringBuilder b = new StringBuilder(toXmlBeg());
		b.append(XmlUtil.setAttribute(0, XK.SORT, getSort().toString()));
		if (getSup() != null) {
			b.append(XmlUtil.setAttribute(0, XK.SUP, getClean(getSup().getId())));
		}
		b.append(">\n");
		b.append(toXmlEnd());
		return b.toString();
	}

	public static Category fromXml(Node node) {
		Category p = new Category();
		fromXmlBeg(node, p);
		p.setSort(XmlUtil.getInteger(node, XK.SORT));
		fromXmlEnd(node, p);
		return p;
	}

	@Override
	@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
	public boolean equals(Object obj) {
		if (!super.equals(obj)) {
			return false;
		}
		Category test = (Category) obj;
		boolean ret = true;
		ret = ret && equalsIntegerNullValue(sort, test.getSort());
		ret = ret && equalsStringNullValue(getName(), test.getName());
		return ret;
	}

	@Override
	public int hashCode() {
		return hashPlus(super.hashCode(), sort, sup);
	}

	@Override
	public int compareTo(AbstractEntity en) {
		Category ca = (Category) en;
		if (hasSup() && (ca.hasSup()) && (!(getSup().equals(ca.getSup())))) {
			return getSup().compareTo(ca.getSup());
		}
		return sort.compareTo(ca.sort);
	}

	public static Category find(List<Category> list, String str) {
		for (Category elem : list) {
			if (elem.getName().equals(str)) {
				return (elem);
			}
		}
		return (null);
	}

	public static Category find(List<Category> list, Long id) {
		for (Category elem : list) {
			if (elem.id.equals(id)) {
				return (elem);
			}
		}
		return (null);
	}

	public static List<String> getDefColumns() {
		List<String> list = AbstractEntity.getDefColumns(Book.TYPE.CATEGORY);
		return (list);
	}

	public static List<String> getTable() {
		List<String> ls = new ArrayList<>();
		String tableName = DAOutil.CATEGORY;
		AbstractEntity.getTable(tableName, ls);
		ls.add(tableName + ",sort,Integer,0");
		ls.add(tableName + ",category_id,Integer,0");
		return (ls);
	}

}
