package com.teamcenter.rac.workflow.commands.dotask;

import java.io.File;
import java.util.ArrayList;

import java.util.List;

import com.connor.nhjs.gy.findpz.WordBean;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
//import com.teamcenter.rac.stylesheet.DesignChange;

public class PzCard {
	
	private static final int START = 6;
	private static final int MAX = 24;
	
	private WordBean Word;
	private String partID;// 修改号
	private String partNub;// 件号
	private String changeName;// 名称
	private String drawingRev;// 图纸版本
	private String processType;// 工艺类型
	private String processCod;// 工艺编码
	private String processRev;// 工艺版本
	private String changeReason;// 修改原因描述
	private String treatSug;// 成品处理意见
	private String articleSug;// 在制品处理意见
	private List<DesignChange> list1  = new ArrayList<DesignChange>();
	private List<DesignChange> list  = new ArrayList<DesignChange>();
	private File file;
	private File file2;

	public FindPzBean bean;

	public PzCard(FindPzBean bean, File file, File file2,TCSession session) throws TCException {
		//  Auto-generated constructor stub
		//System.out.println(bean);
		this.bean = bean;
		this.file = file;
		this.file2 = file2;
		initValue();
	}

	/**
	 * 初始化值
	 * 
	 * @throws TCException
	 */
	private void initValue() throws TCException {
		//  Auto-generated method stub
		this.partID = bean.getPartID() == null ? "":bean.getPartID();
		this.partNub = bean.getPartNub() == null ? "" : bean.getPartNub();
		this.changeName = bean.getChangeName()== null ? "" : bean.getChangeName();
		this.drawingRev = bean.getDrawingRev()== null ? "" : bean.getDrawingRev();
		this.processType = bean.getProcessType()== null ? "" : bean.getProcessType();
		this.processCod = bean.getProcessCod()== null ? "" : bean.getProcessCod();
		this.processRev = bean.getProcessRev()== null ? "" : bean.getProcessRev();
		this.changeReason = bean.getChangeReason()== null ? "" : bean.getChangeReason();
		this.treatSug = bean.getTreatSug()== null ? "" : bean.getTreatSug();
		this.articleSug = bean.getArticleSug()== null ? "" : bean.getArticleSug();
		this.list1 = bean.getChanges();
		this.list = bean.getDescs();

		System.out.println("partID=====" + partID);
		System.out.println("partNub=====" + partNub);
		System.out.println("changeName=====" + changeName);
		System.out.println("drawingRev=====" + drawingRev);
		System.out.println("processType=====" + processType);
		System.out.println("processCod=====" + processCod);
		System.out.println("processRev=====" + processRev);
		System.out.println("changeReason=====" + changeReason);

		System.out.println("treatSug=====" + treatSug);
		System.out.println("articleSug=" + articleSug);
		System.out.println("******************************************************");

	}

	public WordBean writeDataToWord() {
		Word = new WordBean();
		boolean openFlag = Word.openDocument(file.getAbsolutePath());
		if (!openFlag) {
			return null;
		}
		writeData();
		//Word.deleteNullPage();
		//页数
		
		Word.moveDocEnd();
		Word.deleteNullPage();
		int pages = Word.getPages();
		Word.replaceAllText("#pa", pages+"");
		// 输出内容到word
		return Word;
	}

	/**
	 * 输出内容到word
	 */
	private void writeData() {
		// TODO Auto-generated method stub

		System.out.println("==========================更改内容=========" + list.size());
		int allRow = 0;
		int row = START;// 第2行开始
		int row1 = START;// 第2行开始
		int row2 = START;// 第2行开始
		int row3 = START;// 第2行开始
		int table_num = 1;
		int table_num1 = 1;
		int table_num2 = 1;
		int table_num3 = 1;
		int size = list.size();
		for (int i = 0; i < size; i++) {
			Word.moveStart();
			DesignChange descBean = list.get(i);
//			Word.putTxtToCell(table_num1, row1, 1, descBean.getSerialNub()+"");
			String markSTR = descBean.getSerialNub();
			List<String> markList = Util.list2map(markSTR, 50);
			String beforeSTR = descBean.getChangeNub();
			List<String> beforeList = Util.list2map(beforeSTR, 360);
			String afterSTR = descBean.getChangeName();
			List<String> afterList = Util.list2map(afterSTR, 360);
			
			if(beforeList.size()>=afterList.size() && beforeList.size()>=markList.size()) {
				allRow = row + beforeList.size() - 1;
			}else if(afterList.size()>=beforeList.size() && afterList.size()>=markList.size()) {
				allRow = row + afterList.size() - 1;
			}else {
				allRow = row + markList.size() - 1;
			}
			if(i==size-1) {
				allRow--;
			}
			if(allRow > MAX) {
				int page = allRow/MAX ;
				for (int j = 0; j < page; j++) {
					Word.moveDocEnd();
					Word.insertFile(file2.getAbsolutePath());
				}
				row = allRow%MAX + 2 + START;
				table_num++;
			}else {
				row = allRow + 2;
			}
			if(i==size-1) {
				row--;
			}
			for (int j = 0; j < markList.size(); j++) {
//				System.out.println("row2===="+row2);
				Word.putHtmlToCell(table_num1, row1, 1, markList.get(j));
				row1++;
				if(row1 > MAX) {
					table_num1 += 1;
					row1 = START;
				}
			}
			for (int j = 0; j < beforeList.size(); j++) {
//				System.out.println(j+"===="+beforeList.get(j));
				Word.putHtmlToCell(table_num2, row2, 2, beforeList.get(j));
				row2++;
				if(row2 > MAX) {
					table_num2 += 1;
					row2 = START;
				}
			}
//			System.out.println("row2===="+KGenerateGDTHTML.generatePage(beforeList.get(0)));
			for (int j = 0; j < afterList.size(); j++) {
//				System.out.println("row3===="+row3);
				Word.putHtmlToCell(table_num3, row3, 3, afterList.get(j));
				row3++;
				if(row3 > MAX) {
					table_num3 += 1;
					row3 = START;
				}
			}
			if(row > MAX) {
				table_num += 1;
				row = START;
				Word.moveDocEnd();
				Word.insertFile(file2.getAbsolutePath());
			}
			row1 = row;
			row2 = row;
			row3 = row;
			table_num1 = table_num;
			table_num2 = table_num;
			table_num3 = table_num;
		}
		for(int i = 1; i <= table_num; i++) {
			Word.putTxtToCell(i, MAX + 4, 2, "第"+i+"页");
		}
		for (int i = 0; i < list1.size(); i++) {//TODO 
			if(i>5)
				break;
			Word.moveStart();
			DesignChange descBean = list1.get(i);
			Word.putTxtToCell(1, 4, i+2, descBean.getChangeNub());
		}

		// 修改号
		Word.replaceAllText("#partID", partID);
		// 件号
		Word.replaceAllText("#partNub", partNub);

		// 名称
		Word.replaceAllText("#changeName", changeName);

		// 图纸版本
		Word.replaceAllText("#drawingRev", drawingRev);

		// 工艺类型
		Word.replaceAllText("#processType", processType);

		// 工艺编码
		Word.replaceAllText("#processCod", processCod);

		// 工艺版本
		Word.replaceAllText("#processRev", processRev);

		// 修改原因描述
		Word.replaceAllText("#changeReason", changeReason);

		// 成品处理意见
		Word.replaceAllText("#treatSug", treatSug);

		// 在制品处理意见
		Word.replaceAllText("#articleSug", articleSug);

	}
}
