package com.danding.yang800.Adapter;


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

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.danding.yang800.Config.ConfigUtil;
import com.danding.yang800.DocumentActivity;
import com.danding.yang800.Entity.SortListInfo;
import com.danding.yang800.General.SingleRequestQueue;
import com.danding.yang800.Presenter.SelectGoodsActivityPresenter;
import com.danding.yang800.R;
import com.danding.yang800.SelectGoodsActivity;
import com.danding.yang800.Util.BitmapCache;
import com.danding.yang800.Util.BitmapUtil;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.zhy.autolayout.utils.AutoUtils;

public class SelectGoodsAdapter extends MyBaseAdapter<SortListInfo> {
    private RequestQueue mQueue;
    SelectGoodsActivity context;
    SelectGoodsActivityPresenter presenter;
    SharedPreferences sp;
    private ImageLoader mImageLoader;

    public SelectGoodsAdapter(SelectGoodsActivity context) {
        super(context);
        this.context = context;
        mQueue = SingleRequestQueue.getRequestQueue(context);
        mImageLoader = new ImageLoader(mQueue, new BitmapCache());
        sp = context.getSharedPreferences("variable", Context.MODE_PRIVATE);
        presenter = new SelectGoodsActivityPresenter(context, this);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Info info = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.select_goods_item, null);
            info = new Info();
            info.selectgoods_changePricelayout = (RelativeLayout) convertView.findViewById(R.id.selectgoods_changePricelayout);
            info.selectgoods_documentlayout = (RelativeLayout) convertView.findViewById(R.id.selectgoods_documentlayout);
            info.selectgoods_adapterShopIv = (ImageView) convertView.findViewById(R.id.selectgoods_adapterShopIv);
            info.selectgoods_shangjialayout = (RelativeLayout) convertView.findViewById(R.id.selectgoods_shangjialayout);
            info.selectgoods_sharelayout = (RelativeLayout) convertView.findViewById(R.id.selectgoods_sharelayout);
            info.selectgoods_shouyelayout = (RelativeLayout) convertView.findViewById(R.id.selectgoods_shouyelayout);
            info.selectgoods_shangjiaTv = (TextView) convertView.findViewById(R.id.selectgoods_shangjiaTv);
            info.selectgoods_shangjiaIv = (ImageView) convertView.findViewById(R.id.selectgoods_shangjiaIv);
            info.selectGoods_noneIv = (ImageView) convertView.findViewById(R.id.selectGoods_noneIv);
            info.selectgoods_shouyeTv = (TextView) convertView.findViewById(R.id.selectgoods_shouyeTv);
            info.selectgoods_adapternameTv = (TextView) convertView.findViewById(R.id.selectgoods_adapternameTv);
            info.selectgoods_skuMarketPrice = (TextView) convertView.findViewById(R.id.selectgoods_skuMarketPrice);
            info.selectgoods_skuSupplyPrice = (TextView) convertView.findViewById(R.id.selectgoods_skuSupplyPrice);
            info.selectgoods_skuProfit = (TextView) convertView.findViewById(R.id.selectgoods_skuProfit);
            info.selectgoods_skuMarketInventory = (TextView) convertView.findViewById(R.id.selectgoods_skuMarketInventory);
            info.selectgoods_priceStateTv = (TextView) convertView.findViewById(R.id.selectgoods_priceStateTv);
            info.selectgoods_expFreeTv = (TextView) convertView.findViewById(R.id.selectgoods_expFreeTv);
            info.selectgoods_taxFreeTv = (TextView) convertView.findViewById(R.id.selectgoods_taxFreeTv);
            info.selectgoods_shouyeIv = (ImageView) convertView.findViewById(R.id.selectgoods_shouyeIv);


            convertView.setTag(info);
            AutoUtils.autoSize(convertView);
        } else {
            info = (Info) convertView.getTag();
        }
        if (getItem(position).getUrl().contains("http")) {
            info.selectgoods_adapterShopIv.setTag(ConfigUtil.getImageUrl(getItem(position).getUrl(), 210, 210));
            LogUtil.d(getItem(position).getUrl() + "1111" + position);
        } else {
            info.selectgoods_adapterShopIv.setTag("http://");
        }
        info.selectgoods_shangjialayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.initDialog();
                if (getItem(position).getIfup().equals("已上架")) {
                    presenter.rockingDown(sp.getString("storeId", ""), position, getItem(position).getGoodsSn());
                } else {
                    presenter.rockingByOne(sp.getString("storeId", ""), position, getItem(position).getGoodsSn(), getItem(position).getSkuSn()
                            , getItem(position).getSkuMarketPrice());
                }
            }
        });
        info.selectgoods_shouyelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getItem(position).getIfup().equals("已上架")) {
                    if (getItem(position).isShouyexian()) {
                        presenter.homeShow(sp.getString("storeId", ""), position, getItem(position).getGoodsSn(), "0");
                    } else {
                        presenter.homeShow(sp.getString("storeId", ""), position, getItem(position).getGoodsSn(), "1");
                    }
                } else {
                    ToastUtil.show(context, "请先上架商品");
                }
            }
        });
        info.selectgoods_changePricelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getItem(position).getIfup().equals("已上架")) {
                    context.toChangePriceActivity(position, getItem(position).getGoodsSn(), getItem(position).getSkuSn());
                } else {
                    ToastUtil.show(context, "请先上架商品");
                }
            }
        });
        info.selectgoods_sharelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //分享弹出框
                if (getItem(position).getIfup().equals("已上架")) {
                    sp.edit().putBoolean("WXlogin", false).commit();
                    if (!StringUtil.getString(context.storeUrl) || !StringUtil.getString(context.goodsUrl)) {
                        presenter.getShareUrl();
                    }
                    presenter.getStoreName(sp.getString("storeId", ""));
                    context.showWindow(position);
                } else {
                    ToastUtil.show(context, "请先上架商品");
                }

            }
        });

        info.selectgoods_documentlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DocumentActivity.class);
                intent.putExtra("goodsSn", getItem(position).getGoodsSn());
                intent.putExtra("goodsUrl", context.goodsUrl + "?skuSn=" + getItem(position).getStoreSkuSn());
                intent.putExtra("ifUp", getItem(position).getIfup());
                context.startActivity(intent);
            }
        });
        if (getItem(position).isExpFree()) {
            info.selectgoods_expFreeTv.setVisibility(View.VISIBLE);
        } else {
            info.selectgoods_expFreeTv.setVisibility(View.GONE);
        }
        if (getItem(position).isTaxFree()) {
            info.selectgoods_taxFreeTv.setVisibility(View.VISIBLE);
        } else {
            info.selectgoods_taxFreeTv.setVisibility(View.GONE);
        }
        if (getItem(position).getIfup().equals("上架")) {
            info.selectgoods_shangjiaIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.shangjia));
        } else {
            info.selectgoods_shangjiaIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.yishangjia));
        }
        if (getItem(position).isShouyexian()) {
            info.selectgoods_shouyeIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.shouyexian));
            info.selectgoods_shouyeTv.setText("首页显");
        } else {
            info.selectgoods_shouyeIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.feishouyexian));
            info.selectgoods_shouyeTv.setText("非首页显");
        }
        if (getItem(position).isStorePriceChange()) {
            info.selectgoods_priceStateTv.setVisibility(View.VISIBLE);
        } else {
            info.selectgoods_priceStateTv.setVisibility(View.GONE);
        }
        info.selectgoods_shangjiaTv.setText(getItem(position).getIfup());
        info.selectgoods_adapternameTv.setText(getItem(position).getName());
        info.selectgoods_skuMarketPrice.setText("￥" + getItem(position).getSkuMarketPrice());
        info.selectgoods_skuSupplyPrice.setText(getItem(position).getSkuSupplyPrice());
        info.selectgoods_skuProfit.setText(getItem(position).getSkuProfit());
        if (getItem(position).getStats().equals("3")) {
            info.selectGoods_noneIv.setVisibility(View.VISIBLE);
            info.selectgoods_skuMarketInventory.setText(" 仅剩" + 0 + " ");
        } else {
            info.selectGoods_noneIv.setVisibility(View.GONE);
            info.selectgoods_skuMarketInventory.setText(" 仅剩" + getItem(position).getSkuMarketInventory() + " ");
        }
        ImageLoader.ImageListener listener = BitmapUtil.getImageListener(info.selectgoods_adapterShopIv, R.drawable.huisebeijing);
        if (getItem(position).getUrl().contains("http")) {
            mImageLoader.get(ConfigUtil.getImageUrl(getItem(position).getUrl(), 210, 210), listener);
        } else {
            info.selectgoods_adapterShopIv.setImageResource(R.drawable.huisebeijing);
        }
//        Glide.with(context).load(ConfigUtil.getImageUrl(getItem(position).getUrl(), 210, 210)).error(R.drawable.huisebeijing).
//                diskCacheStrategy(DiskCacheStrategy.ALL).into(info.selectgoods_adapterShopIv);
        return convertView;
    }

    class Info {
        RelativeLayout selectgoods_changePricelayout;
        RelativeLayout selectgoods_documentlayout;
        RelativeLayout selectgoods_shangjialayout;
        RelativeLayout selectgoods_sharelayout;
        RelativeLayout selectgoods_shouyelayout;
        ImageView selectgoods_adapterShopIv;
        ImageView selectgoods_shangjiaIv;
        ImageView selectGoods_noneIv;
        TextView selectgoods_shangjiaTv;
        ImageView selectgoods_shouyeIv;
        TextView selectgoods_shouyeTv;
        TextView selectgoods_adapternameTv;
        TextView selectgoods_skuMarketPrice;
        TextView selectgoods_skuSupplyPrice;
        TextView selectgoods_skuProfit;
        TextView selectgoods_skuMarketInventory;
        TextView selectgoods_priceStateTv;
        TextView selectgoods_expFreeTv;
        TextView selectgoods_taxFreeTv;
    }


}
