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.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.MyShopActivity;
import com.danding.yang800.Presenter.MyshopActivityPresenter;
import com.danding.yang800.R;
import com.danding.yang800.Util.BitmapCache;
import com.danding.yang800.Util.BitmapUtil;
import com.danding.yang800.Util.StringUtil;
import com.danding.yang800.Util.ToastUtil;
import com.zhy.autolayout.utils.AutoUtils;

public class MyShopAdapter extends MyBaseAdapter<SortListInfo> {
    MyShopActivity context;
    MyshopActivityPresenter presenter;
    SharedPreferences sp;
    private ImageLoader mImageLoader;
    private RequestQueue mQueue;

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


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Info info = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.myshop_shop, null);
            info = new Info();
            info.myshop_changePricelayout = (RelativeLayout) convertView.findViewById(R.id.myshop_changePricelayout);
            info.myshop_documentlayout = (RelativeLayout) convertView.findViewById(R.id.myshop_documentlayout);
            info.myshop_adapterShopIv = (ImageView) convertView.findViewById(R.id.myshop_adapterShopIv);
            info.myshop_shangjialayout = (RelativeLayout) convertView.findViewById(R.id.myshop_shangjialayout);
            info.myshop_sharelayout = (RelativeLayout) convertView.findViewById(R.id.myshop_sharelayout);
            info.myshop_shouyelayout = (RelativeLayout) convertView.findViewById(R.id.myshop_shouyelayout);
            info.myshop_shangjiaTv = (TextView) convertView.findViewById(R.id.myshop_shangjiaTv);
            info.myshop_shangjiaIv = (ImageView) convertView.findViewById(R.id.myshop_shangjiaIv);
            info.myshop_noneIv = (ImageView) convertView.findViewById(R.id.myshop_noneIv);
            info.myshop_shouyeTv = (TextView) convertView.findViewById(R.id.myshop_shouyeTv);
            info.myshop_adapternameTv = (TextView) convertView.findViewById(R.id.myshop_adapternameTv);
            info.myshop_skuMarketPrice = (TextView) convertView.findViewById(R.id.myshop_skuMarketPrice);
            info.myshop_skuProfit = (TextView) convertView.findViewById(R.id.myshop_skuProfit);
            info.myshop_skuMarketInventory = (TextView) convertView.findViewById(R.id.myshop_skuMarketInventory);
            info.myshop_priceStateTv = (TextView) convertView.findViewById(R.id.myshop_priceStateTv);
            info.myshop_expFreeTv = (TextView) convertView.findViewById(R.id.myshop_expFreeTv);
            info.myshop_taxFreeTv = (TextView) convertView.findViewById(R.id.myshop_taxFreeTv);
            info.myshop_shouyeIv = (ImageView) convertView.findViewById(R.id.myshop_shouyeIv);
//            info.myshop_shangjialayout.setOnClickListener(new OnConvertViewClickListener(convertView, R.id.ab__id_adapter_item_position) {
//                @Override
//                public void onClickCallBack(View registedView, int positionIds) {
//                    int posi = Integer.parseInt(String.valueOf(registedView.getTag(positionIds)));
//                    String ifup = getItem(posi).getIfup();
//                    ImageView myshop_shangjiaIv = (ImageView) registedView
//                            .findViewById(R.id.myshop_shangjiaIv);
//                    TextView myshop_shangjiaTv = (TextView) registedView
//                            .findViewById(R.id.myshop_shangjiaTv);
//                    if (ifup.equals("已上架")) {
//                        myshop_shangjiaTv.setText("上架");
//                        myshop_shangjiaIv.setImageDrawable(context.getResources().getDrawable(
//                                R.drawable.shangjia));
//                        getItem(posi).setIfup("上架");
//                    } else {
//                        myshop_shangjiaTv.setText("已上架");
//                        getItem(posi).setIfup("已上架");
//                        myshop_shangjiaIv.setImageDrawable(context.getResources().getDrawable(
//                                R.drawable.yishangjia));
//                    }
//                }
//            });
            convertView.setTag(info);
            AutoUtils.autoSize(convertView);
        } else {
            info = (Info) convertView.getTag();
        }
        if (getItem(position).getStats().equals("3")) {
            info.myshop_noneIv.setVisibility(View.VISIBLE);
            info.myshop_skuMarketInventory.setText(" 仅剩" + 0 + " ");
        } else {
            info.myshop_noneIv.setVisibility(View.GONE);
            info.myshop_skuMarketInventory.setText(" 仅剩" + getItem(position).getSkuMarketInventory() + " ");
        }
        // convertView.setTag(R.id.ab__id_adapter_item_position, position);
        info.myshop_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());
                }
            }
        });
        info.myshop_shouyelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getItem(position).getIfup().equals("已上架")) {
                    context.initDialog();
                    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.myshop_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.myshop_sharelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //分享弹出框
                if (!StringUtil.getString(context.storeUrl) || !StringUtil.getString(context.goodsUrl)) {
                    presenter.getShareUrl();
                }
                context.showWindow(position);
            }
        });

        info.myshop_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.myshop_expFreeTv.setVisibility(View.VISIBLE);
        } else {
            info.myshop_expFreeTv.setVisibility(View.GONE);
        }
        if (getItem(position).isTaxFree()) {
            info.myshop_taxFreeTv.setVisibility(View.VISIBLE);
        } else {
            info.myshop_taxFreeTv.setVisibility(View.GONE);
        }
        if (getItem(position).getIfup().equals("上架")) {
            info.myshop_shangjiaIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.shangjia));
        } else {
            info.myshop_shangjiaIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.yishangjia));
        }
        if (getItem(position).isShouyexian()) {
            info.myshop_shouyeIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.shouyexian));
            info.myshop_shouyeTv.setText("首页显");
        } else {
            info.myshop_shouyeIv.setImageDrawable(context.getResources().getDrawable(
                    R.drawable.feishouyexian));
            info.myshop_shouyeTv.setText("非首页显");
        }
        if (getItem(position).isStorePriceChange()) {
            info.myshop_priceStateTv.setVisibility(View.VISIBLE);
        } else {
            info.myshop_priceStateTv.setVisibility(View.GONE);
        }
        info.myshop_adapterShopIv.setTag(ConfigUtil.getImageUrl(getItem(position).getUrl(), 210, 210));
        info.myshop_shangjiaTv.setText(getItem(position).getIfup());
        info.myshop_adapternameTv.setText(getItem(position).getName());
        info.myshop_skuMarketPrice.setText("￥" + getItem(position).getSkuMarketPrice());
        info.myshop_skuProfit.setText(getItem(position).getSkuProfit());

        ImageLoader.ImageListener listener = BitmapUtil.getImageListener(info.myshop_adapterShopIv, R.drawable.huisebeijing);
        if (getItem(position).getUrl().contains("http")) {
            mImageLoader.get(ConfigUtil.getImageUrl(getItem(position).getUrl(), 210, 210), listener);
        } else {
            mImageLoader.get("", listener);
        }
//        Glide.with(context).load(getItem(position).getUrl()).error(R.mipmap.ic_launcher).
// diskCacheStrategy(DiskCacheStrategy.SOURCE).into(info.myshop_adapterShopIv);
        return convertView;
    }

    class Info {
        RelativeLayout myshop_changePricelayout;
        RelativeLayout myshop_documentlayout;
        RelativeLayout myshop_shangjialayout;
        RelativeLayout myshop_sharelayout;
        RelativeLayout myshop_shouyelayout;
        ImageView myshop_adapterShopIv;
        ImageView myshop_shangjiaIv;
        TextView myshop_shangjiaTv;
        ImageView myshop_shouyeIv;
        ImageView myshop_noneIv;
        TextView myshop_shouyeTv;
        TextView myshop_adapternameTv;
        TextView myshop_skuMarketPrice;
        TextView myshop_skuProfit;
        TextView myshop_skuMarketInventory;
        TextView myshop_priceStateTv;
        TextView myshop_expFreeTv;
        TextView myshop_taxFreeTv;
    }
}
