package com.zhiwei.readWechat;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.Document;

import com.zhiwei.servlet.RwController;
import com.zhiwei.util.Config;
import com.zhiwei.util.ReadWechatConnect;
import com.zw.entity.ExcelFile;
import com.zw.entity.Excels;
import com.zw.entity.ReadWechat;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import sendmail.SendMailUtil;

public class WechatReadAndWrite {
	private final static Log log = LogFactory.getLog(WechatReadAndWrite.class);
	public static Excels e;
	public static boolean cut = false;
	public static String url = Config.getVal("basefile");
	public static boolean runStrat = false;
	public static int cutCount = 0;
	public static String host = Config.getVal("socketIp_wechat");
	public static int port = Integer.valueOf(Config.getVal("socketPort_wechat"));
	private static ReadWechatConnect m = new ReadWechatConnect();

	public static void insert(ExcelFile ef) {
		m.insertFileWechat(ef);
	}

	public static List<ExcelFile> findEf() {
		List<ExcelFile> list = m.findFile();
		return list;
	}

	public static String findById(String id) {
		return m.findByid(id);
	}

	public static void writes(List<ReadWechat> list, String mail) throws Exception {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String inurl = url + (int) (Math.random() * 99) + sdf.format(cal.getTime()) + ".xls";
		System.out.println(inurl);
		File tempFile = new File(inurl);
		if (!tempFile.exists()) {
			tempFile.createNewFile();
		}
		WritableWorkbook workbook = Workbook.createWorkbook(tempFile);
		WritableSheet sheet = workbook.createSheet("table1", 0);
		Label l = null;
		WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 14, WritableFont.NO_BOLD, false,
				UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
		WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
		titleFormat.setAlignment(Alignment.CENTRE);
		WritableFont urlf = new WritableFont(WritableFont.ARIAL, 11, WritableFont.NO_BOLD, false,
				UnderlineStyle.NO_UNDERLINE, Colour.BLUE);
		WritableCellFormat urlFormat = new WritableCellFormat(urlf);
		urlFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
		int seq = 0;
		l = new Label(0, seq, "来源");
		sheet.addCell(l);
		l = new Label(1, seq, "时间");
		sheet.addCell(l);
		l = new Label(2, seq, "标题");
		sheet.addCell(l);
		l = new Label(3, seq, "地址");
		sheet.addCell(l);
		l = new Label(4, seq, "评论数");
		sheet.addCell(l);
		l = new Label(5, seq, "点赞数");
		sheet.addCell(l);
		l = new Label(6, seq, "阅读数");
		sheet.addCell(l);

		for (ReadWechat w : list) {
			seq++;
			l = new Label(0, seq, w.getSource());
			sheet.addCell(l);
			l = new Label(1, seq, sdf.format(new Date()));
			sheet.addCell(l);
			l = new Label(2, seq, w.getTitle());
			sheet.addCell(l);
			l = new Label(3, seq, w.getUrl());
			sheet.addCell(l);
			if (w.getComment_num() != null) {
				l = new Label(4, seq, w.getComment_num());
				sheet.addCell(l);
			}
			if (w.getZan_num() != null) {
				l = new Label(5, seq, w.getZan_num());
				sheet.addCell(l);
			}
			if (w.getRead_num() != null) {
				l = new Label(6, seq, w.getRead_num());
				sheet.addCell(l);
			}
		}
		int column = 0;
		sheet.setColumnView(column++, 20);
		sheet.setColumnView(column++, 25);
		sheet.setColumnView(column++, 45);
		sheet.setColumnView(column++, 25);
		sheet.setColumnView(column++, 30);
		sheet.setColumnView(column++, 30);
		sheet.setColumnView(column++, 30);
		workbook.write();
		workbook.close();
		log.info("内容写入成功");
		SendMailUtil.sendMessage(mail, inurl, "ccxe");
		ExcelFile ef = new ExcelFile();
		ef.setMail(mail);
		ef.setUrl(inurl);
		insert(ef);
		System.out.println("发送完成");
	}

	public synchronized static void work() {
		runStrat = true;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		while (true) {
			if (RwController.weChatList.size() > 0) {
				try {
					e = RwController.weChatList.get(0);
					if (e != null) {
						e.setStartDate(sdf.format(new Date()));
						e.setState("运行中···");
						List<ReadWechat> list = ReadExcel.getExcel(e.getUrl());
						log.info("数据入库");
						m.insertReadWechat(list);// 存储
						ServerClient.needSend = false;
						while (!ServerClient.needSend) {
							Thread.sleep(1000 * 30);
						}
						Thread.sleep(1000 * 30);
						Map<String, ReadWechat> map = new HashMap<>();
						for (Document doc : m.findReadWechat()) {
							ReadWechat rw = new ReadWechat();
							rw.setComment_num(String.valueOf(doc.get("comment_num")));
							rw.setUrl(String.valueOf(doc.get("url")));
							rw.setRead_num(String.valueOf(doc.get("read_num")));
							rw.setZan_num(String.valueOf(doc.get("zan_num")));
							map.put(String.valueOf(doc.get("url")), rw);
						}
						for (ReadWechat rw : list) {
							if (map.get(rw.getUrl()) != null) {
								ReadWechat rwt = map.get(rw.getUrl());
								rw.setComment_num(rwt.getComment_num());
								rw.setRead_num(rwt.getRead_num());
								rw.setZan_num(rwt.getZan_num());
							}
						}
						writes(list, e.getMail());
						m.remove();
						RwController.weChatList.remove(RwController.weChatList.get(0));
						e = null;
					}
				} catch (NullPointerException e) {
					e.printStackTrace();
					RwController.weChatList.remove(RwController.weChatList.get(0));
				} catch (Exception e1) {
					e1.printStackTrace();
					RwController.weChatList.remove(RwController.weChatList.get(0));
				}
			} else {
				try {
					Thread.sleep(1000 * 60);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	}

}
