package com.danding.yang800.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Entity.OrderInfo;
import com.danding.yang800.LogisticsActivity;
import com.danding.yang800.OrderManageActivity;
import com.danding.yang800.R;
import com.zhy.autolayout.utils.AutoUtils;

public class AllrderAdapter extends MyBaseAdapter<OrderInfo> {
    //    private RequestQueue mQueue;
    SharedPreferences sp;
    OrderManageActivity context;
//    private ImageLoader mImageLoader;

    public AllrderAdapter(OrderManageActivity context) {
        super(context);
        this.context = context;
//        mQueue = SingleRequestQueue.getRequestQueue(context);
//        mImageLoader = new ImageLoader(mQueue, new BitmapCache());
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
    }

    String order_needpay;
    String order_tax;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Info info = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.order_info_layout, null);
            info = new Info();
            info.order_infolayout = (LinearLayout) convertView
                    .findViewById(R.id.order_infolayout);
            info.orderinfo_statusTV = (TextView) convertView
                    .findViewById(R.id.orderinfo_statusTV);
            info.order_infoorderdeal = (LinearLayout) convertView
                    .findViewById(R.id.order_infoorderdeal);
            info.order_infopayorder = (TextView) convertView
                    .findViewById(R.id.order_infopayorder);
            info.order_creatTimeTv = (TextView) convertView
                    .findViewById(R.id.order_creatTimeTv);
            info.order_needpayTv = (TextView) convertView
                    .findViewById(R.id.order_needpayTv);
            info.order_shippongInfotV = (TextView) convertView
                    .findViewById(R.id.order_shippongInfotV);

            convertView.setTag(info);
            AutoUtils.autoSize(convertView);
        } else {
            info = (Info) convertView.getTag();
        }
        View view;
        info.order_infolayout.removeAllViews();
        LinearLayout lineLayout = new LinearLayout(context);
        lineLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lineLayout.setLayoutParams(layoutParams);
        for (int i = 0; i < getItem(position).getOrderContent().length; i++) {
            view = LayoutInflater.from(context).inflate(
                    R.layout.shopinfo_layout, null);
            TextView shop_infotv = (TextView) view
                    .findViewById(R.id.shop_infotv);
            order_needpay = getItem(position).getAmount();
            order_tax = getItem(position).getTotalTax();
            shop_infotv.setText(getItem(position).getOrderContent()[i]);
            TextView mycollet_moneyTv = (TextView) view
                    .findViewById(R.id.mycollet_moneyTv);
            mycollet_moneyTv.setText("￥" + getItem(position).getShopPrice()[i]);
            TextView mycollet_numTv = (TextView) view
                    .findViewById(R.id.mycollet_numTv);
            mycollet_numTv.setText("x" + getItem(position).getShopNumber()[i]);
            ImageView shop_IV = (ImageView) view.findViewById(R.id.shop_IV);
//            shop_IV.setTag(getItem(position).getUrl()[i]);
//            ImageLoader.ImageListener listener = BitmapUtil.getImageListener(shop_IV,R.drawable.huisebeijing);
//            mImageLoader.get(ConfigUtil.getImageUrl(getItem(position).getUrl()[i]), listener);
            Glide.with(context).load(getItem(position).getUrl()[i]).
                    error(R.drawable.huisebeijing).diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(shop_IV);
            lineLayout.addView(view);
            if (getItem(position).getOrderContent().length > 1 && i < getItem(position).getOrderContent().length - 1) {
                View myview = LayoutInflater.from(context).inflate(
                        R.layout.order_manage_addview, null);
                lineLayout.addView(myview);
            }
        }
        info.order_infolayout.addView(lineLayout);
        info.order_infopayorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LogisticsActivity.class);
                intent.putExtra("packageSn", getItem(position).getPackageSn());
                context.startActivity(intent);
            }
        });
        info.order_needpayTv.setText(order_needpay);
        info.order_shippongInfotV.setText("(含税费￥" + order_tax + " 运费￥" + getItem(position).getTotalExpPrice() + ")");
        info.orderinfo_statusTV.setText(getItem(position).getStatusDesc());
        info.order_creatTimeTv.setText(getItem(position).getCreateTime());
        if (getItem(position).isShowExpress()) {
//           orderinfo_statusTV.setText("待付款");
            info.order_infoorderdeal.setVisibility(View.VISIBLE);
        } else {
            info.order_infoorderdeal.setVisibility(View.GONE);
        }
        return convertView;
    }

    class Info {
        LinearLayout order_infolayout;
        LinearLayout order_infoorderdeal;
        TextView orderinfo_statusTV;
        TextView order_infopayorder;
        TextView order_needpayTv;
        TextView order_shippongInfotV;
        TextView order_creatTimeTv;
    }

}
