package com.ibeetl.admin.core.rbac.tree;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ibeetl.admin.core.entity.DouYinChannelEntity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ChannelItem implements ChannelTreeItem{
	DouYinChannelEntity sysChannel = null;
	List<ChannelItem> children = new ArrayList<>();
	@JsonIgnore
	ChannelItem parent = null;
	String name ;
	public ChannelItem(DouYinChannelEntity sysChannel){
		this.sysChannel = sysChannel;
		this.name = sysChannel!=null?sysChannel.getName():null;
	}
	
	
	public void setParent(ChannelItem parent){
		this.parent = parent;
		parent.children.add(this);
	}
	
	public List<ChannelItem> getChildren(){
		return this.children;
	}
	
	public Long getId(){
		return sysChannel.getId();
	}
	
	public DouYinChannelEntity getData(){
		return this.sysChannel;
	}
	
	/**
	 * 查找某个指定的子功能
	 * @param functionId
	 * @return
	 */
	public ChannelItem findChild(long id){
		if(sysChannel.getId()==id){
			return this;
		}
		for(ChannelItem item:children){
			ChannelItem find = item.findChild(id);
			if(find!=null){
				return find;
			}
		}
		return null;
		
	}
	
	/**
	 * 查找所有的子功能
	 * @return
	 */
	public List<ChannelItem> findAllItem(){
		List<ChannelItem> all = new LinkedList<>();
		findAllChildItem(all,this);
		return all;
	}
	
	public List<Long> findAllChildrenId(){
		List<ChannelItem> items =findAllItem();
		List<Long> children = new ArrayList<Long>();
		for(ChannelItem item:items){
			children.add(item.getId());
		}
		return children;
	}
	
	
	private void findAllChildItem(List<ChannelItem> all,ChannelItem parent){
		for(ChannelItem item:parent.children){
			all.add(item);
			findAllChildItem(all,item);
		}
		
	}
	
	
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((sysChannel == null) ? 0 : sysChannel.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ChannelItem other = (ChannelItem) obj;
		if (sysChannel == null) {
            return other.sysChannel == null;
		} else return sysChannel.equals(other.sysChannel);
    }


	@Override
	public String toString() {
		return "ChannelItem [sysChannel=" + sysChannel.getName() + "," +sysChannel.getOrderchannel()+"," +this.children.size()+"]";
	}


	@Override
	public String getName() {
		return sysChannel==null?"":sysChannel.getName();
	}

	
	
	
	
	
	

}
