package nc.ui.pubapp.uif2app.actions;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.UUID;

import nc.bs.framework.common.NCLocator;
import nc.bs.pub.filesystem.IFileSystemActionConst;
import nc.bs.uif2.BusinessExceptionAdapter;
import nc.bs.uif2.IActionCode;
import nc.desktop.ui.WorkbenchEnvironment;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.md.MDBaseQueryFacade;
import nc.md.data.access.NCObject;
import nc.md.model.IBean;
import nc.md.model.IBusinessEntity;
import nc.md.model.MetaDataException;
import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.bill.BillItem;
import nc.ui.pub.filesystem.ThFileManageUI;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.UIState;
import nc.ui.uif2.actions.ActionInitializer;
import nc.ui.uif2.editor.BillForm;
import nc.ui.uif2.model.AbstractUIAppModel;
import nc.vo.bd.meta.IBDObject;
import nc.vo.bd.meta.NCObject2BDObjectAdapter;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.uap.rbac.FuncSubInfo;
import nc.vo.uap.rbac.profile.FunctionPermProfileManager;

public class InvoiceUploadAction extends NCAction implements IFileSystemActionConst{
	private static final long serialVersionUID = 4674049033047277460L;
	private AbstractUIAppModel model;
	public BillForm editor;
	
	public BillForm getEditor() {
		return editor;
	}

	public void setEditor(BillForm editor) {
		this.editor = editor;
	}

	public InvoiceUploadAction() {
		super();
		ActionInitializer.initializeAction(this, IActionCode.FILE);
		 this.setBtnName("Ìí¼Ó·¢Æ±");
	        this.setCode("invoiceUploadAction");
	}

	public IBDObject createBDObject(Object obj) {
		if (obj == null) {
			return null;
		} else if (obj instanceof String) {
			return null;
		} else {
			try {
				String className = null;
				if (obj instanceof AbstractBill) {
					className = ((AbstractBill) obj).getParentVO().getClass().getName();
				} else {
					className = obj.getClass().getName();
				}

				IBean bean = MDBaseQueryFacade.getInstance().getBeanByFullClassName(className);
				if (bean == null) {
					return null;
				} else {
					Map<String, String> name_path_map = ((IBusinessEntity) bean)
							.getBizInterfaceMapInfo(IBDObject.class.getName());
					return new NCObject2BDObjectAdapter(NCObject.newInstance(obj), name_path_map);
				}
			} catch (MetaDataException var5) {
				throw new BusinessExceptionAdapter(var5);
			}
		}
	}

	public void doAction(ActionEvent e) throws Exception {
		Object selectedData = this.model.getSelectedData();
		BillCardPanel cpanel=editor.getBillCardPanel();
		String vdef17 = (String) cpanel.getHeadItem("vdef17").getValueObject();
		String rootPath = null;
		if(vdef17 != null){
			rootPath = vdef17;
		}else {
			rootPath = UUID.randomUUID().toString();
			cpanel.setHeadItem("vdef17", rootPath);
		}
		IBDObject target = this.createBDObject(selectedData);
		//if (target != null) {
			String userCode = WorkbenchEnvironment.getInstance().getLoginUser().getUser_code();
			String funcode = this.model.getContext().getNodeCode();
			String pk_group = this.model.getContext().getPk_group();
			//FileManageUI ui = FileManageUIFactory.getFileManagePnl(rootPath, userCode, funcode, pk_group);
			FuncSubInfo funcsubinfo = FunctionPermProfileManager.getInstance().getProfile(userCode).getFuncSubInfo(funcode);
		    ThFileManageUI ui = new ThFileManageUI(rootPath,editor);
			setActionPermission(ui,funcsubinfo);
		      
			ui.setTreeRootVisible(false);
			UIDialog dlg = new UIDialog(this.model.getContext().getEntranceUI(),
					NCLangRes4VoTransl.getNCLangRes().getStrByID("pubapp_0", "0pubapp-0129"));
			dlg.getContentPane().setLayout(new BorderLayout());
			dlg.getContentPane().add(ui, "Center");
			dlg.setResizable(true);
			dlg.setSize(600, 400);
			dlg.showModal();
		}
	//}

	public AbstractUIAppModel getModel() {
		return this.model;
	}

	public void setModel(AbstractUIAppModel model) {
		this.model = model;
		this.model.addAppEventListener(this);
	}

	protected boolean isActionEnable() {
		boolean ret = this.model.getSelectedData() != null && this.model.getUiState() != UIState.EDIT
				&& this.model.getUiState() != UIState.ADD;
		return true;
	}
	
	private static void setActionPermission(ThFileManageUI fui,FuncSubInfo funcsubinfo){		
	    fui.setUploadFileEnable(funcsubinfo.isValidButton(UPLOAD));
		fui.setCreateNewFolderEnable(funcsubinfo.isValidButton(UPLOAD));
		fui.setDownloadFileEnable(funcsubinfo.isValidButton(DOWNLOAD));
		fui.setShowFileEnable(funcsubinfo.isValidButton(BROWSE));
		fui.setDeleteNodeEnable(funcsubinfo.isValidButton(DELETE));
		fui.setRenameNodeEnable(funcsubinfo.isValidButton(RENAME));
	}
}