package com.fxy.logic;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.elvishew.xlog.XLog;
import com.fxy.baselibrary.views.MarqueeTextView;
import com.fxy.R;
import com.lzy.ninegrid.NineGridView;

import org.json.JSONException;
import org.json.JSONObject;

public class ViewLogic {
    private LinearLayout linearLayout = null;

    public ViewLogic(LinearLayout llTallyInfo) {
        linearLayout = llTallyInfo;
    }

    //设置理货数据
    public void setTallyData(JSONObject orderObject){
        MarqueeTextView mtOrderNumber= linearLayout.findViewById(R.id.mt_order_number);
        MarqueeTextView mtCustomerName= linearLayout.findViewById(R.id.mt_customer_name);
        MarqueeTextView mtReceiverCountry= linearLayout.findViewById(R.id.mt_receiver_country);
        MarqueeTextView mtDeliveryAddress= linearLayout.findViewById(R.id.mt_delivery_address);
        MarqueeTextView mtLogisticsProducts= linearLayout.findViewById(R.id.mt_logistics_products);
        MarqueeTextView mtForecastQty= linearLayout.findViewById(R.id.mt_forecast_qty);
        MarqueeTextView mtStorageLocation= linearLayout.findViewById(R.id.mt_storage_location);
        MarqueeTextView mtPutInTag= linearLayout.findViewById(R.id.mt_put_in_tag);


        try {
            mtOrderNumber.setText(orderObject.has("order_number")?orderObject.getString("order_number"):"");
            mtCustomerName.setText(orderObject.has("customer_code")?orderObject.getString("customer_code"):"");
            mtReceiverCountry.setText(orderObject.has("receive_country")?orderObject.getString("receive_country"):"");
            mtDeliveryAddress.setText(orderObject.has("delivery_address")?orderObject.getString("delivery_address"):"");
            mtLogisticsProducts.setText(orderObject.has("product_name")?orderObject.getString("product_name"):"");
            mtStorageLocation.setText(orderObject.has("domestic_wms_location")?orderObject.getString("domestic_wms_location"):"");
            mtForecastQty.setText(orderObject.has("forecast_pieces")?orderObject.getString("forecast_pieces"):"");
        } catch (JSONException e) {
            XLog.e("参数错误：",e.getMessage());
            e.printStackTrace();
        }

        //mtPutInTag.setText(inboundBean.getInboundNumber());
    }

    //进行一些特殊设置
    public void setTallyMore(JSONObject paramObject){
        try {
            if (paramObject.has("has_label")){
                MarqueeTextView mtPutInTag= linearLayout.findViewById(R.id.mt_put_in_tag);
                //ll_docker_name
                if (paramObject.getString("has_label").equals("Y")){
                    mtPutInTag.setText("有");
                }
                if (paramObject.getString("has_label").equals("N")){
                    mtPutInTag.setText("无");
                }
                linearLayout.findViewById(R.id.ll_put_id_tag).setVisibility(View.VISIBLE);
            }
            if (paramObject.has("docker_name")){
                MarqueeTextView  mtDockerName = linearLayout.findViewById(R.id.mt_docker_name);
                mtDockerName.setText(paramObject.getString("docker_name"));
                linearLayout.findViewById(R.id.ll_docker_name).setVisibility(View.VISIBLE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置查验数据
     * @param orderObject
     */
    public void setCheckData(JSONObject orderObject){
        MarqueeTextView mtOrderNumber= linearLayout.findViewById(R.id.mt_order_number);
        MarqueeTextView mtChannelName= linearLayout.findViewById(R.id.mt_channel_name);
        MarqueeTextView mtLocationCode= linearLayout.findViewById(R.id.mt_location_code);
        MarqueeTextView mtInboundPieces= linearLayout.findViewById(R.id.mt_inbound_pieces);
        MarqueeTextView mtInboundDate= linearLayout.findViewById(R.id.mt_inbound_date);
        try {
            linearLayout.findViewById(R.id.ll_channel_name).setVisibility(View.GONE);
            linearLayout.findViewById(R.id.tv_to_check).setVisibility(View.GONE);

            mtOrderNumber.setText(orderObject.has("order_number")?orderObject.getString("order_number"):"");
            mtChannelName.setText(orderObject.has("channel_name")?orderObject.getString("channel_name"):"");
            mtLocationCode.setText(orderObject.has("domestic_wms_location")?orderObject.getString("domestic_wms_location"):"");
            mtInboundPieces.setText(orderObject.has("inbound_pieces")?orderObject.getInt("inbound_pieces")+"":"");
            mtInboundDate.setText(orderObject.has("inbound_date")?orderObject.getString("inbound_date"):"");
        } catch (JSONException e) {
            XLog.e("参数错误：",e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 设置查验数据
     * @param orderObject
     */
    public void setGoodsData(JSONObject orderObject,JSONObject goodsObject){
        MarqueeTextView mtOrderNumber= linearLayout.findViewById(R.id.mt_order_number);
        MarqueeTextView mtShipmentId= linearLayout.findViewById(R.id.mt_shipment_id);
        MarqueeTextView mtPackageNumber= linearLayout.findViewById(R.id.mt_package_number);
        MarqueeTextView mtBoxQty= linearLayout.findViewById(R.id.mt_box_qty);
        MarqueeTextView mtCategoryCnName= linearLayout.findViewById(R.id.mt_category_cn_name);
        MarqueeTextView mtCategoryEnName= linearLayout.findViewById(R.id.mt_category_en_name);
        MarqueeTextView mtBrand= linearLayout.findViewById(R.id.mt_brand);
        MarqueeTextView mtModel_number= linearLayout.findViewById(R.id.mt_model_number);
        MarqueeTextView mtCartonSize= linearLayout.findViewById(R.id.mt_carton_size);

        try {
            mtOrderNumber.setText(orderObject.has("order_number")?orderObject.getString("order_number"):"");

            mtShipmentId.setText(goodsObject.getString("shipment_id"));
            mtPackageNumber.setText(goodsObject.getString("package_number"));
            mtBoxQty.setText(goodsObject.getString("box_qty"));
            mtCategoryCnName.setText(goodsObject.getString("cn_name"));
            mtCategoryEnName.setText(goodsObject.getString("en_name"));
            mtBrand.setText(goodsObject.getString("brand"));
            mtModel_number.setText( goodsObject.getString("type"));
            double box_length = goodsObject.getDouble("box_length");
            double box_width = goodsObject.getDouble("box_width");
            double box_height = goodsObject.getDouble("box_height");
            String boxSize = box_length+"*"+box_width+"*"+box_height;
            mtCartonSize.setText(boxSize);


        } catch (JSONException e) {
            XLog.e("参数错误：",e.getMessage());
            e.printStackTrace();
        }
    }

    public void setStorageInfo(JSONObject outboundObject){
        MarqueeTextView mt_outbound_number= linearLayout.findViewById(R.id.mt_outbound_number);
        MarqueeTextView mt_order_qty= linearLayout.findViewById(R.id.mt_order_qty);
        MarqueeTextView mt_container_code= linearLayout.findViewById(R.id.mt_container_code);
        MarqueeTextView mt_so_number= linearLayout.findViewById(R.id.mt_so_number);
        MarqueeTextView mt_seal_number= linearLayout.findViewById(R.id.mt_seal_number);

        try {
            mt_outbound_number.setText(outboundObject.getString("outbound_number"));
            mt_order_qty.setText(outboundObject.getString("order_qty"));
            mt_container_code.setText(outboundObject.getString("container_code"));
            mt_so_number.setText(outboundObject.getString("so_number"));
            mt_seal_number.setText(outboundObject.getString("seal_number"));
        } catch (JSONException e) {
            XLog.e("参数错误：",e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     *
     * @param orderObject
     */
    @SuppressLint("SetTextI18n")
    public void setOutbound(JSONObject dataObject, JSONObject orderObject ){
        MarqueeTextView mtOrderNumber= linearLayout.findViewById(R.id.mt_order_number);
        MarqueeTextView mtContainerNumber= linearLayout.findViewById(R.id.mt_container_number);
        TextView tvNotNum= linearLayout.findViewById(R.id.tv_not_num);
        TextView tvHasNum= linearLayout.findViewById(R.id.tv_has_num);
        TextView tvCargoWeight= linearLayout.findViewById(R.id.tv_cargo_weight);
        TextView tvWeightUnit= linearLayout.findViewById(R.id.tv_weight_unit);
        TextView tvVolume= linearLayout.findViewById(R.id.tv_volume);
        TextView tvVolumeUnit= linearLayout.findViewById(R.id.tv_volume_unit);


        try {
            if (orderObject ==null){
                linearLayout.findViewById(R.id.ll_order_number).setVisibility(View.GONE);
                linearLayout.findViewById(R.id.ll_container_number).setVisibility(View.GONE);
            }else{
                mtOrderNumber.setText(orderObject.has("outbound_number")?orderObject.getString("outbound_number"):"");
                mtContainerNumber.setText(orderObject.has("container_code")?orderObject.getString("container_code"):"");
            }



            String hasStr = dataObject.getString("has");
            String notStr = dataObject.getString("not");

            if (hasStr.isEmpty() || hasStr.equals("[]")){
                tvHasNum.setText("0");
                tvCargoWeight.setText("0");
                tvWeightUnit.setText("KG");
                tvVolume.setText("0");
                tvVolumeUnit.setText("CM");
            }else{
                JSONObject hasObject = dataObject.getJSONObject("has");

                tvHasNum.setText(Integer.toString(hasObject.getInt("total")));
                tvCargoWeight.setText(hasObject.getString("inbound_weight"));
                tvWeightUnit.setText(hasObject.getString("weight_unit"));
                tvVolume.setText(hasObject.getString("inbound_volume"));
                tvVolumeUnit.setText(hasObject.getString("volume_unit"));
            }

            if (notStr.isEmpty() || notStr.equals("[]")){
                tvNotNum.setText("0");
            }else{
                JSONObject notObject = dataObject.getJSONObject("not");
                tvNotNum.setText(notObject.getInt("total")+"");
            }

        } catch (JSONException e) {
            XLog.e("参数错误：",e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 显示网路图片
     */
    public static class GlideImageLoder implements NineGridView.ImageLoader {
        public GlideImageLoder(Context context, ImageView ivGoodsImg, String url) {
            this.onDisplayImage(context,ivGoodsImg,url);
        }
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.drawable.ic_default_img)
                    .error(R.drawable.ic_default_img)
                    .priority(Priority.HIGH);
            Glide.with(context).load(url).apply(options).into(imageView);
        }

        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }

}