package com.gx.obe.struct.composite;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import com.gx.obe.bind.prop.Prop;
import com.gx.obe.component.comparison.Column;
import com.gx.obe.component.comparison.ComparisonData;
import com.gx.obe.component.comparison.ComparisonTable;
import com.gx.obe.component.rx.RxSwt;
import com.gx.obe.component.utils.CompositeUtils;
import com.gx.obe.components.core.Constants;
//import com.gx.obe.components.core.vo.TableXML;
//import com.gx.obe.components.core.vo.TableXML.Tr;
//import com.gx.obe.components.core.vo.TableXML.Tr.Td;
import com.gx.obe.http.json.JsonUtil;
import com.gx.obe.struct.beans.TableXML2;
import com.gx.obe.struct.beans.Td;
import com.gx.obe.struct.beans.TemplateTable;
import com.gx.obe.struct.beans.Tr;
import com.gx.obe.struct.service.TemplateTableService;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Supplier;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TemplateTableComparisonComposite extends Composite {
	
	private final TemplateTableService templateTableService = new TemplateTableService();
	
	private final Prop<ComparisonData> comparisonData = new Prop<>();
	
	public TemplateTableComparisonComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		comparisonData.bind(t -> {
			CompositeUtils.disposeChildren(this);
			new ComparisonTable(this, t);
			this.layout();
		});
	}
	
	public void refresh(String tenderId, String relChapterType, String dataCode, List<Supplier> supplierList) {
		RxSwt.run(() -> getComparisonData(tenderId, relChapterType, dataCode, supplierList)).checkWidget(this).exe(comparisonData::set);
	}
	
	private ComparisonData getComparisonData(String tenderId, String relChapterType, String dataCode, List<Supplier> supplierList) {
		List<TemplateTable> templateTableList = templateTableService.getTemplateTableList(tenderId, relChapterType, dataCode);
		if (CollectionUtils.isNull(templateTableList)) {
			return null;
		}
//		List<Tr> trList = null;
		int rowCount = 0;
		Map<String, TemplateTable> templateTableMap = templateTableList.stream().collect(Collectors.toMap(TemplateTable::getSupplierId, Function.identity()));
		for(TemplateTable templateTable : templateTableList) {
			TableXML2 tableXML = JsonUtil.jsonToBean(templateTable.getTableInfo(), TableXML2.class);
			if(rowCount < tableXML.getBodyList().size()) {
				rowCount = tableXML.getBodyList().size();
			}
		}
		TemplateTable templateTable = templateTableMap.get(Constants.BIDDING_ID);
		if (templateTable == null) {
			return null;
		}
		TableXML2 tableXML = JsonUtil.jsonToBean(templateTable.getTableInfo(), TableXML2.class);
		if (tableXML == null) {
			return null;
		}
		List<Tr> headList = tableXML.getHeadList();
		if (headList.size() != 1) {
			return null;
		}
		Tr head = headList.get(0);
		OptionalInt indexCol = IntStream.range(0, head.getTds().length).filter(i -> "序号".equals(head.getTds()[i].getContent())).findAny();
		if (!indexCol.isPresent()) {
			return null;
		}
		ComparisonData comparisonData = new ComparisonData();
//		for(Tr tr : tableXML.getBodyList()) {
//			System.out.println(getContent(tr, indexCol.getAsInt()));
//			comparisonData.addRow(getContent(tr, indexCol.getAsInt()));
//		}
		for(int index = 0; index < rowCount; index++) {
			comparisonData.addRow((index + 1) + "");
		}
//		tableXML.getBodyList().forEach(body -> Optional.ofNullable(getContent(body, indexCol.getAsInt())).filter(StringUtils::isNotEmpty).ifPresent(comparisonData::addRow));
		for(Supplier supplier : supplierList) {
			comparisonData.addTitleCol(new Column(supplier.getId(), supplier.getSupplierName()));
		}
//		supplierList.forEach(supplier -> {
//			comparisonData.addTitleCol(new Column(supplier.getId(), supplier.getSupplierName()));
//		});
		
		for(int index = 0;index < head.getTds().length; index++) {
			comparisonDataInit(templateTableMap, tableXML, head, indexCol.getAsInt(), index, supplierList, comparisonData);
		}
//		IntStream.range(0, head.getTds().length).forEach(i -> {
//			comparisonDataInit(templateTableMap, tableXML, head, indexCol.getAsInt(), i, supplierList, comparisonData);
//		});
		return comparisonData;
	}
	
	private void comparisonDataInit(Map<String, TemplateTable> templateTableMap, TableXML2 tableXML, Tr head, int indexCol, int col, List<Supplier> supplierList, ComparisonData comparisonData) {
		Td td = head.getTds()[col];
//		supplierList.forEach(supplier -> {
//			supplierComparisonDataInit(templateTableMap, indexCol, col, supplier, comparisonData);
//		});
		for(Supplier supplier : supplierList) {
			supplierComparisonDataInit(templateTableMap, indexCol, col, supplier, comparisonData);
		}
//		comparisonData.addItemCol(new Column(col + "", td.getContent()));
		comparisonData.addItemCol(new Column(col + "", td.getContent()));
//		if (td.isNeedFill()) {
//		} else {
//			comparisonData.addHeadCol(new Column(col + "", td.getContent()));
//			tableXML.getBodyList().forEach(body -> {
//				comparisonData.putHeadContent(getContent(body, indexCol), col + "", getContent(body, col));
//			});
//		}
	}
	
	private void supplierComparisonDataInit(Map<String, TemplateTable> templateTableMap, int indexCol, int col, Supplier supplier, ComparisonData comparisonData) {
		TemplateTable supplierTemplateTable = templateTableMap.get(supplier.getId());
		if (supplierTemplateTable == null) {
			return;
		}
		TableXML2 supplierTableXML = JsonUtil.jsonToBean(supplierTemplateTable.getTableInfo(), TableXML2.class);
		if (supplierTableXML == null) {
			return;
		}
		for(Tr  tes: supplierTableXML.getBodyList()) {
			comparisonData.putComparisonContent(getContent(tes, indexCol), supplier.getId(), col + "", getContent(tes, col));
		}
//		System.out.println(comparisonData.getComparisonContentMap());
//		System.out.println();
//		supplierTableXML.getBodyList().forEach(body -> {
//			comparisonData.putComparisonContent(getContent(body, indexCol), supplier.getId(), col + "", getContent(body, col));
//		});
	}
	
	private String getContent(Tr tr, int col) {
//		System.out.println(col);
//		System.out.println(tr.getTds()[col].getContent());
		return tr.getTds()[col].getContent();
	}
	
	@Override
	protected void checkSubclass() {}
	

	
}