package com.bonus.canteen.core.common.thirdutils.feie;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.bonus.canteen.core.common.config.sys.ThirdApiUrlProperties;
import com.bonus.common.houqin.constant.LeConstants;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class FeiePrint {
   private static final Logger log = LoggerFactory.getLogger(FeiePrint.class);
   @Autowired
   private ThirdApiUrlProperties thirdApiUrlProperties;
   public static final String URL = "/Api/Open/";
   public static final String USER = "346995697@qq.com";
   public static final String UKEY = "5TNhb7hct5Brx5Rr";
   public static final String API_NAME_KEY = "apiname";

   private static String signature(String stime) {
      return DigestUtils.sha1Hex("346995697@qq.com5TNhb7hct5Brx5Rr" + stime);
   }

   public FeiePrintResponse queryOrderState(String orderid) {
      List<NameValuePair> nvps = this.getGeneralVarList();
      nvps.add(new BasicNameValuePair("apiname", "Open_queryOrderState"));
      nvps.add(new BasicNameValuePair("orderid", orderid));
      FeiePrintResponse feiePrintResponse = this.execute(nvps, "Open_queryOrderState");
      if ("ok".equals(feiePrintResponse.getMsg()) && 0 == feiePrintResponse.getRet()) {
         feiePrintResponse.setMsg("已打印");
         feiePrintResponse.setCode("ok");
      } else {
         feiePrintResponse.setMsg("失败");
         feiePrintResponse.setCode(Boolean.FALSE.toString());
      }

      return feiePrintResponse;
   }

   public FeiePrintResponse addprinter(String sn, String key, String printname) {
      List<NameValuePair> nvps = this.getGeneralVarList();
      nvps.add(new BasicNameValuePair("apiname", "Open_printerAddlist"));
      nvps.add(new BasicNameValuePair("printerContent", sn + "#" + key + "#" + printname + "#"));
      FeiePrintResponse feiePrintResponse = this.execute(nvps, "Open_printerAddlist");
      FeiePrintResponse feiePrintResponse1 = (FeiePrintResponse)JSON.parseObject(feiePrintResponse.getData(), FeiePrintResponse.class);
      feiePrintResponse.setNo(feiePrintResponse1.getNo());
      feiePrintResponse.setOk(feiePrintResponse1.getOk());
      feiePrintResponse.setCode(Boolean.FALSE.toString());
      if ("ok".equals(feiePrintResponse.getMsg()) && 0 == feiePrintResponse.getRet()) {
         if ("[]".equals(feiePrintResponse.getOk())) {
            if (feiePrintResponse.getNo().contains("添加过")) {
               feiePrintResponse.setMsg("已添加,请勿重复添加");
            } else {
               feiePrintResponse.setMsg("添加失败===》请检查参数：sn码/key是否正确");
            }
         } else {
            feiePrintResponse.setMsg("已添加");
            feiePrintResponse.setCode("ok");
         }
      } else if ("ok".equals(feiePrintResponse.getMsg()) && 1001 == feiePrintResponse.getRet()) {
         feiePrintResponse.setMsg("参数错误");
      }

      return feiePrintResponse;
   }

   public FeiePrintResponse print(String sn, String content, String times) {
      List<NameValuePair> nvps = this.getGeneralVarList();
      nvps.add(new BasicNameValuePair("apiname", "Open_printMsg"));
      nvps.add(new BasicNameValuePair("sn", sn));
      nvps.add(new BasicNameValuePair("content", content));
      nvps.add(new BasicNameValuePair("times", times));
      FeiePrintResponse feiePrintResponse = this.execute(nvps, "Open_printMsg");
      if ("ok".equals(feiePrintResponse.getMsg()) && 0 == feiePrintResponse.getRet()) {
         feiePrintResponse.setMsg(feiePrintResponse.getData());
         feiePrintResponse.setCode("ok");
      } else {
         feiePrintResponse.setMsg("打印失败");
         feiePrintResponse.setCode(Boolean.FALSE.toString());
      }

      return feiePrintResponse;
   }

   public FeiePrintResponse printLabelMsg(String sn, String content, String times) {
      List<NameValuePair> nvps = this.getGeneralVarList();
      nvps.add(new BasicNameValuePair("apiname", "Open_printLabelMsg"));
      nvps.add(new BasicNameValuePair("sn", sn));
      nvps.add(new BasicNameValuePair("content", content));
      nvps.add(new BasicNameValuePair("times", times));
      FeiePrintResponse feiePrintResponse = this.execute(nvps, "Open_printLabelMsg");
      if ("ok".equals(feiePrintResponse.getMsg()) && 0 == feiePrintResponse.getRet()) {
         feiePrintResponse.setMsg(feiePrintResponse.getData());
         feiePrintResponse.setCode("ok");
      } else {
         feiePrintResponse.setMsg("打印失败");
         feiePrintResponse.setCode(Boolean.FALSE.toString());
      }

      return feiePrintResponse;
   }

   public FeiePrintResponse delPrinter(String sn) {
      List<NameValuePair> nvps = this.getGeneralVarList();
      nvps.add(new BasicNameValuePair("apiname", "Open_printerDelList"));
      nvps.add(new BasicNameValuePair("snlist", sn));
      FeiePrintResponse feiePrintResponse = this.execute(nvps, "Open_printerDelList");
      if ("ok".equals(feiePrintResponse.getMsg()) && 0 == feiePrintResponse.getRet()) {
         feiePrintResponse.setMsg(feiePrintResponse.getData());
         feiePrintResponse.setCode("ok");
      } else {
         feiePrintResponse.setMsg("参数错误");
         feiePrintResponse.setCode(Boolean.FALSE.toString());
      }

      return feiePrintResponse;
   }

   public Integer queryPrinterStatus(String sn) {
      List<NameValuePair> nvps = this.getGeneralVarList();
      nvps.add(new BasicNameValuePair("apiname", "Open_queryPrinterStatus"));
      nvps.add(new BasicNameValuePair("sn", sn));
      FeiePrintResponse feiePrintResponse = this.execute(nvps, "Open_queryPrinterStatus");
      if ("ok".equals(feiePrintResponse.getMsg()) && 0 == feiePrintResponse.getRet()) {
         return ObjectUtil.equal("离线。", feiePrintResponse.getData()) ? LeConstants.COMMON_NO : LeConstants.COMMON_YES;
      } else {
         return LeConstants.COMMON_NO;
      }
   }

   private List<NameValuePair> getGeneralVarList() {
      List<NameValuePair> nvps = CollUtil.newArrayList(new NameValuePair[0]);
      nvps.add(new BasicNameValuePair("user", "346995697@qq.com"));
      String stime = String.valueOf(System.currentTimeMillis() / 1000L);
      nvps.add(new BasicNameValuePair("stime", stime));
      nvps.add(new BasicNameValuePair("sig", signature(stime)));
      return nvps;
   }

   private FeiePrintResponse execute(List<NameValuePair> nvps, String apiname) {
      RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();
      CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
      HttpPost post = new HttpPost(this.thirdApiUrlProperties.getFeieapi() + "/Api/Open/");
      CloseableHttpResponse response = null;
      String result = null;

      try {
         post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
         response = httpClient.execute(post);
         int statecode = response.getStatusLine().getStatusCode();
         if (statecode == 200) {
            HttpEntity httpentity = response.getEntity();
            if (httpentity != null) {
               result = EntityUtils.toString(httpentity);
            }
         }
      } catch (Exception var26) {
         log.info("【飞鹅打印错误】{}", var26.getMessage(), var26);
      } finally {
         try {
            if (response != null) {
               response.close();
            }
         } catch (IOException var25) {
         }

         try {
            post.abort();
         } catch (Exception var24) {
         }

         try {
            httpClient.close();
         } catch (IOException var23) {
         }

      }

      log.info("【飞鹅】{} 返回：{}", apiname, result);
      return (FeiePrintResponse)JSON.parseObject(result, FeiePrintResponse.class);
   }
}
