package com.goafanti.common.controller;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goafanti.cognizance.service.OrgActivityCostService;
import com.goafanti.cognizance.service.OrgActivityService;
import com.goafanti.cognizance.service.OrgFinanceService;
import com.goafanti.cognizance.service.OrgHonorDatumService;
import com.goafanti.cognizance.service.OrgIntellectualPropertyService;
import com.goafanti.cognizance.service.OrgRatepayService;
import com.goafanti.cognizance.service.OrgStandardService;
import com.goafanti.cognizance.service.OrgTechAchievementService;
import com.goafanti.cognizance.service.OrgTechCenterDetailService;
import com.goafanti.cognizance.service.OrgTechProductService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.dao.UserMapper;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.OrgActivity;
import com.goafanti.common.model.OrgActivityCost;
import com.goafanti.common.model.OrgFinance;
import com.goafanti.common.model.OrgHonorDatum;
import com.goafanti.common.model.OrgHumanResource;
import com.goafanti.common.model.OrgIntellectualProperty;
import com.goafanti.common.model.OrgRatepay;
import com.goafanti.common.model.OrgStandard;
import com.goafanti.common.model.OrgTechAchievement;
import com.goafanti.common.model.OrgTechCenterDetail;
import com.goafanti.common.model.OrgTechProduct;
import com.goafanti.common.model.PatentInfo;
import com.goafanti.common.model.TechProject;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.SHA256Util;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.copyright.bo.CopyrightInfoDetail;
import com.goafanti.copyright.service.CopyrightInfoService;
import com.goafanti.customer.bo.CustomerOrganizationDetailBo;
import com.goafanti.patent.service.PatentInfoService;
import com.goafanti.techproject.service.TechProjectService;
import com.goafanti.user.service.OrgHumanResourceService;
import com.goafanti.user.service.OrganizationIdentityService;
import com.goafanti.user.service.UserService;

@Controller
@RequestMapping("/open/")
public class PreviewController extends BaseApiController {

	@Resource
	private PatentInfoService				patentInfoService;
	@Resource
	private UserService						userService;
	@Resource
	private CopyrightInfoService			copyrightInfoService;
	@Resource
	private TechProjectService				techProjectService;
	@Resource
	private OrgTechCenterDetailService		orgTechCenterDetailService;
	@Resource
	private OrgActivityService				orgActivityService;
	@Resource
	private OrgActivityCostService			orgActivityCostService;
	@Resource
	private OrgTechProductService			orgTechProductService;
	@Resource
	private OrgIntellectualPropertyService	orgIntellectualPropertyService;
	@Resource
	private OrgTechAchievementService		orgTechAchievementService;
	@Resource
	private OrgRatepayService				orgRatepayService;
	@Resource
	private OrgFinanceService				orgFinanceService;
	@Resource
	private OrgHumanResourceService			OrgHumanResourceService;
	@Resource
	private OrgHonorDatumService			OrgHonorDatumService;
	@Resource
	private OrgStandardService				orgStandardService;
	@Resource
	private OrganizationIdentityService		organizationIdentityService;
	@Resource
	private UserMapper	userMapper;

	@Value(value = "${aesSecretKey}")
	private String							secretKey	= null;

	@RequestMapping(value = "/preview", method = RequestMethod.GET)
	public Result preview(HttpServletResponse response, String sign, String temp, String token2, String auth) {
		Result res = new Result();
		if (!StringUtils.isNumeric(temp)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "预览"));
		} else if (DateUtils.truncatedMinuteDiffTo(Calendar.getInstance().getTime(),
				new Date(Long.parseLong(temp))) > 19) {
			res.getError().add(buildError(ErrorConstants.NO_AUTH_ERROR));
		} else if (!SHA256Util.toHash(sign + "|" + token2 + "|" + secretKey, temp).equals(auth)) {
			res.getError().add(buildError(ErrorConstants.NO_AUTH_ERROR));
		} else {
			AttachmentType attachmentType = AttachmentType.getField(sign);
			switch (attachmentType) {
			case PATENT_PRORY_STATEMENT:
			case PATENT_WRITING:
			case AUTHORIZATION_NOTICE:
			case ORGANIZATION_APPLICATION:
				CustomerOrganizationDetailBo u=userMapper.findOrganizationCustomerDetail(token2);
				TechProject p=new TechProject();
				p.setApprovalUrl(u.getPojectApplicationUrl());
				handleTechProjectPreview(response, p, attachmentType);
				break;
			case PATENT_CERTIFICATE:
				PatentInfo pi = patentInfoService.selectByPrimaryKey(token2);
				if (pi == null) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handlePatentPreview(response, pi, attachmentType);
				}
				break;
			case LAST_YEAR_RATEPAY:
			case RATEPAY:
				OrgRatepay ratepay = orgRatepayService.selectByPrimaryKey(token2);
				if (null == ratepay) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleRatepayPreview(response, ratepay, attachmentType);
				}
				break;
			case COPYRIGHT_AUTH:
			case COPYRIGHT_APPLY:
				CopyrightInfoDetail ci = copyrightInfoService.findByPrimaryKey(token2);
				if (null == ci) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleCopyrightPreview(response, ci, attachmentType);
				}
				break;
			case TECH_PROJECT:
				TechProject tp = techProjectService.selectByPrimaryKey(token2);
				if (null == tp) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleTechProjectPreview(response, tp, attachmentType);
				}
				break;
			case PROTOCOL:
				OrgTechCenterDetail otcd = orgTechCenterDetailService.selectByPrimaryKey(token2);
				if (null == otcd) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleTechCenterDetailPreview(response, otcd, attachmentType);
				}
				break;
			case PROOF:
				OrgActivity oa = orgActivityService.selectOrgActivityByPrimaryKey(token2);
				if (null == oa) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleActivityPreview(response, oa, attachmentType);
				}
				break;
			case ACTIVITY_COST_ACCOUNT:
				OrgActivityCost oac = orgActivityCostService.selectByPrimaryKey(token2);
				if (null == oac) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleActivityCostPreview(response, oac, attachmentType);
				}
				break;
			case TECH_PRODUCT:
				OrgTechProduct otp = orgTechProductService.selectByPrimaryKey(token2);
				if (null == otp) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleTechProductPreview(response, otp, attachmentType);
				}
				break;
			case PROPERTY_RIGHT:
				OrgIntellectualProperty oip = orgIntellectualPropertyService.selectByPrimaryKey(token2);
				if (null == oip) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleIntellectualPropertyPreview(response, oip, attachmentType);
				}
				break;
			case ACHIEVEMENT:
				OrgTechAchievement ota = orgTechAchievementService.selectByPrimaryKey(token2);
				if (null == ota) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleAchievementPreview(response, ota, attachmentType);
				}
				break;
			case FINANCE:
				OrgFinance of = orgFinanceService.selectByPrimaryKey(token2);
				if (null == of) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleFinancePreview(response, of, attachmentType);
				}
				break;
			case ROSTER:
			case SOCIAL_SECURITY:
				OrgHumanResource ohr = OrgHumanResourceService.selectByPrimaryKey(token2);
				if (null == ohr) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleHumanResourcePreview(response, ohr, attachmentType);
				}
				break;
			case HONOR:
				OrgHonorDatum ohd = OrgHonorDatumService.selectByPrimaryKey(token2);
				if (null == ohd) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleHonorPreview(response, ohd, attachmentType);
				}
				break;
			case STANDARD:
				OrgStandard os = orgStandardService.selectByPrimaryKey(token2);
				if (null == os) {
					res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "预览"));
					return res;
				} else {
					handleStandardPreview(response, os, attachmentType);
				}
				break;
			default:
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "预览"));
			}
		}
		return res;
	}

	private void handlePatentPreview(HttpServletResponse response, PatentInfo pi, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.PATENT_WRITING) {
			downloadFile(response, pi.getPatentWritingDownloadFileName(), pi.getPatentWritingUrl());
		} else if (attachmentType == AttachmentType.AUTHORIZATION_NOTICE) {
			downloadFile(response, pi.getAuthorizationNoticeDownloadFileName(), pi.getAuthorizationNoticeUrl());
		} else if (attachmentType == AttachmentType.PATENT_CERTIFICATE) {
			downloadFile(response, pi.getPatentCertificateDownloadFileName(), pi.getPatentCertificateUrl());
		} else if (attachmentType == AttachmentType.PATENT_PRORY_STATEMENT) {
			downloadFile(response, pi.getPatentProryStatementDownloadFileName(), pi.getPatentProryStatementUrl());
		} else if (attachmentType == AttachmentType.ORGANIZATION_APPLICATION) {
			downloadFile(response, pi.getPatentProryStatementDownloadFileName(), pi.getPojectApplicationUrl());
		}
	}

	private void handleRatepayPreview(HttpServletResponse response, OrgRatepay ratepay, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.LAST_YEAR_RATEPAY || attachmentType == AttachmentType.RATEPAY) {
			downloadFile(response, ratepay.getTaxReturnDownloadFileName(), ratepay.getTaxReturnUrl());
		}
	}

	private void handleCopyrightPreview(HttpServletResponse response, CopyrightInfoDetail ci,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.COPYRIGHT_AUTH) {
			downloadFile(response, ci.getCertificateUrlDownloadFileName(), ci.getCertificateUrl());
		} else if (attachmentType == AttachmentType.COPYRIGHT_APPLY) {
			downloadFile(response, ci.getApplicationUrlDownloadFileName(), ci.getApplicationUrl());
		}
	}

	private void handleTechProjectPreview(HttpServletResponse response, TechProject tp, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.TECH_PROJECT||attachmentType == AttachmentType.ORGANIZATION_APPLICATION) {
			downloadFile(response, tp.getApprovalDownloadFileName(), tp.getApprovalUrl());
		}
	}

	private void handleTechCenterDetailPreview(HttpServletResponse response, OrgTechCenterDetail otcd,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.PROTOCOL) {
			downloadFile(response, otcd.getProtocolDownloadFileName(), otcd.getProtocolUrl());
		}
	}

	private void handleActivityPreview(HttpServletResponse response, OrgActivity oa, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.PROOF) {
			downloadFile(response, oa.getProofDownloadFileName(), oa.getProofUrl());
		}
	}

	private void handleActivityCostPreview(HttpServletResponse response, OrgActivityCost oac,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.ACTIVITY_COST_ACCOUNT) {
			downloadFile(response, oac.getAccountDownloadFileName(), oac.getAccountUrl());
		}
	}

	private void handleTechProductPreview(HttpServletResponse response, OrgTechProduct otp,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.TECH_PRODUCT) {
			downloadFile(response, otp.getAccountDownloadFileName(), otp.getAccountUrl());
		}
	}

	private void handleIntellectualPropertyPreview(HttpServletResponse response, OrgIntellectualProperty oip,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.PROPERTY_RIGHT) {
			downloadFile(response, oip.getPropertyRightDownloadFileName(), oip.getPropertyRightUrl());
		}
	}

	private void handleAchievementPreview(HttpServletResponse response, OrgTechAchievement ota,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.ACHIEVEMENT) {
			downloadFile(response, ota.getEnclosureDownloadFileName(), ota.getEnclosureUrl());
		}
	}

	private void handleFinancePreview(HttpServletResponse response, OrgFinance of, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.FINANCE) {
			downloadFile(response, of.getFinanceDownloadFileName(), of.getFinanceUrl());
		}
	}

	private void handleHumanResourcePreview(HttpServletResponse response, OrgHumanResource ohr,
			AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.ROSTER) {
			downloadFile(response, ohr.getRosterDownloadFileName(), ohr.getRosterUrl());
		} else if (attachmentType == AttachmentType.SOCIAL_SECURITY) {
			downloadFile(response, ohr.getSocialSecurityDownloadFileName(), ohr.getSocialSecurityUrl());
		}
	}

	private void handleHonorPreview(HttpServletResponse response, OrgHonorDatum ohd, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.HONOR) {
			downloadFile(response, ohd.getEnclosureDownloadFileName(), ohd.getEnclosureUrl());
		}
	}

	private void handleStandardPreview(HttpServletResponse response, OrgStandard os, AttachmentType attachmentType) {
		if (attachmentType == AttachmentType.STANDARD) {
			downloadFile(response, os.getStandardDownloadFileName(), os.getEnclosureUrl());
		}
	}
}
