package com.jianshui.invoice.service.impl.adapter.response;

import com.alibaba.fastjson.JSONObject;
import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.exception.jianshui.JianshuiParamErrorException;
import com.jianshui.common.utils.DictUtils;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.bean.BeanUtils;
import com.jianshui.invoice.domain.BillInfo;
import com.jianshui.invoice.domain.Invoice;
import com.jianshui.invoice.domain.InvoiceDetail;
import com.jianshui.invoice.domain.Redinfo;
import com.jianshui.invoice.domain.dto.HXResponse;
import com.jianshui.invoice.domain.dto.adapter.response.aisino_jn.AisinoJnInvoiceDetailResponse;
import com.jianshui.invoice.domain.dto.adapter.response.aisino_jn.AisinoJnInvoiceResponse;
import com.jianshui.invoice.service.IInvoiceResponseService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 航信出口报文适配器
 * @Author 巩权林
 * @Date 2022/3/19 21:28
 **/
@Component("jn_aisino_invoice_response_adapter")
public class AisinoInvoiceResponseAdapterImpl implements IInvoiceResponseService {

    @Override
    public JSONObject response(HXResponse result, Companyservice companyservice, String query) {

        // 如果是开具
        if (StringUtils.equals("add", query)) {
            return result;
        }

        // 如果是发票查询
        if (StringUtils.equals("query", query)) {
            // 判断有无data
            Object datas = result.getData();

            // 如果datas不为Null
            if (datas != null) {
                // 如果返回将诶过为invioce
                List<AisinoJnInvoiceResponse> responses = new ArrayList<>();
                if (datas instanceof List) {
                    for (int ij = 0; ij < ((List<?>) datas).size(); ij++) {
                        Object data = ((List<?>) datas).get(ij);
                        // 转为HxInvoice
                        Invoice invoice = (Invoice) data;
                        if (invoice == null) {
                            continue;
                        }
                        AisinoJnInvoiceResponse invoiceResponse = new AisinoJnInvoiceResponse();
                        BeanUtils.copyProperties(invoice, invoiceResponse);
                        String invoiceType = DictUtils.getDictLabel("invoice_type", invoice.getInvoiceType());
                        if (invoiceType != null) {
                            invoiceResponse.setInvoiceType(invoiceType);
                        }

                        // 处理发票状态

                        // 处理Detail
                        List<InvoiceDetail> detailList = invoice.getInvoiceDetailList();
                        if (detailList != null) {
                            List<AisinoJnInvoiceDetailResponse> invoiceDetailResponses = new ArrayList<>();
                            for (int i = 0; i < detailList.size(); i++) {
                                AisinoJnInvoiceDetailResponse response = new AisinoJnInvoiceDetailResponse();
                                InvoiceDetail tempDetail = detailList.get(i);
                                BeanUtils.copyProperties(tempDetail, response);
                                Integer hsbz = tempDetail.getHsbz();
                                if (hsbz == 0) {
                                    response.setHsbz("false");
                                    response.setTaxamt(null);
                                    // 如果不含税
                                } else {
                                    // 如果含税
                                    response.setHsbz("true");
                                    response.setTaxfreeamt(response.getTaxamt());
                                }
                                invoiceDetailResponses.add(response);
                            }
                            invoiceResponse.setInvoiceDetailList(invoiceDetailResponses);
                        }
                        responses.add(invoiceResponse);
                    }

                    JSONObject queryResult = new JSONObject();
                    queryResult.put("result", result.getMessage());
                    queryResult.put("list", responses);
                    return queryResult;
                }

            }
            JSONObject queryResult = new JSONObject();
            queryResult.put("result", result.getMessage());
            queryResult.put("list", new ArrayList<>());
        }

        // 如果是开具红票
        if (StringUtils.equals("add_redinfo", query)) {
            return result;
        }
        if (StringUtils.equals("find_redinfo", query)) {
            return result;
        }

        // 如果是下载红票
        if (StringUtils.equals("download_redinfo", query)) {

            Object datas = result.getData();

            // 如果datas不为Null
            if (datas != null) {
                // 如果返回将诶过为invioce
                // List<AisinoJnInvoiceResponse> responses = new ArrayList<>();
                // if (datas instanceof List) {
                // }
            }

            return result;
        }

        // 如果是打印
        if (StringUtils.equals("printer", query)) {

            Object datas = result.getData();

            // 如果datas不为Null
            if (datas != null) {
                // 如果返回将诶过为invioce
                // List<AisinoJnInvoiceResponse> responses = new ArrayList<>();
                // if (datas instanceof List) {
                // }
            }

            return result;
        }

        // 作废查询
        if (StringUtils.equals("deprecate_query", query)) {
            Object datas = result.getData();
            if (datas != null) {
                JSONObject dataJson = (JSONObject) datas;
                // String status = dataJson.getString("status");
                JSONObject result2 = new JSONObject();
                result2.put("message", "操作完成");
                result2.put("status", "0000");
                result2.put("data", dataJson);
                // if (StringUtils.equals(status, "3")) {
                //     result2.put("code", "0000");
                //     result2.put("msg", "作废成功");
                // } else if (StringUtils.equals(status, "1")) {
                //     result2.put("code", "9998");
                //     result2.put("msg", "作废中");
                // } else {
                //     result2.put("code", "9998");
                //     result2.put("msg", "作废失败");
                // }
                return result2;
            }
        }

        if (StringUtils.equals("sign_e_seal", query)) {
            AjaxResult resp = AjaxResult.success();
            Object data = result.getData();
            if (!StringUtils.equals("0000", result.getStatus())) {
                resp = AjaxResult.error(result.getMessage());
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }
            JSONObject dataJson = (JSONObject) data;
            if (StringUtils.equals("0000", dataJson.getString("CODE"))) {
                // result.put("code", "200");
                // result.put("data", dataJson.getJSONObject("data"));
                resp.put("data", dataJson.getJSONObject("DATA"));
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }else{
                // result.put("code", dataJson.getString("CODE"));
                // result.put("message", dataJson.getJSONObject("MESSAGE"));
                resp.put("code", dataJson.getString("CODE"));
                resp.put("message", dataJson.getString("MESSAGE"));
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }
        }
        if (StringUtils.equals("register_e_seal", query)) {
            AjaxResult resp = AjaxResult.success();
            Object data = result.getData();
            if (!StringUtils.equals("0000", result.getStatus())) {
                resp = AjaxResult.error(result.getMessage());
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }
            JSONObject dataJson = (JSONObject) data;
            if (StringUtils.equals("0000", dataJson.getString("CODE"))) {
                // result.put("code", "200");
                // result.put("data", dataJson.getJSONObject("data"));
                resp.put("data", dataJson.getJSONObject("DATA"));
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }else{
                // result.put("code", dataJson.getString("CODE"));
                // result.put("message", dataJson.getJSONObject("MESSAGE"));
                resp.put("code", dataJson.getString("CODE"));
                resp.put("message", dataJson.getString("MESSAGE"));
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }
        }
        if (StringUtils.equals("create_e_seal", query)) {
            AjaxResult resp = AjaxResult.success();
            Object data = result.getData();
            if (!StringUtils.equals("0000", result.getStatus())) {
                resp = AjaxResult.error(result.getMessage());
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }
            JSONObject dataJson = (JSONObject) data;
            if (StringUtils.equals("0000", dataJson.getString("CODE"))) {
                // result.put("code", "200");
                // result.put("data", dataJson.getJSONObject("data"));
                resp.put("data", dataJson.getJSONObject("DATA"));
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }else{
                // result.put("code", dataJson.getString("CODE"));
                // result.put("message", dataJson.getJSONObject("MESSAGE"));
                resp.put("code", dataJson.getString("CODE"));
                resp.put("message", dataJson.getString("MESSAGE"));
                return JSONObject.parseObject(JSONObject.toJSONString(resp));
            }
        }


        return result;
    }

    @Override
    public Object response(JianshuiParamErrorException result, Companyservice companyservice) {
        // JSONObject myResult = new JSONObject();
        // myResult.put("status", result.getErrorCode());
        // myResult.put("message", result.getMsg());
        // return myResult;
        AjaxResult resp = AjaxResult.error(result.getErrorCode(), null);
        resp.put("msg", result.getMsg());
        return resp;
    }

    @Override
    public AjaxResult callback(BillInfo billInfo, Invoice invoice, Redinfo redinfo, Companyservice companyservice, String src) {
        return null;
    }
}
