package com.danding.yang800.Adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.danding.yang800.BaseActivity;
import com.danding.yang800.Entity.MarketPlatInfo;
import com.danding.yang800.Fragment.MarketPlatformFrag;
import com.danding.yang800.Presenter.MarketPlatformFragPresenter;
import com.danding.yang800.R;
import com.zhy.autolayout.utils.AutoUtils;

public class MarketPlatAdapter extends MyBaseAdapter<MarketPlatInfo> {
    BaseActivity context;
    MarketPlatformFrag frag;
    MarketPlatformFragPresenter presenter;

    public MarketPlatAdapter(BaseActivity context, MarketPlatformFrag frag) {
        super(context);
        this.context = context;
        this.frag = frag;
        presenter = new MarketPlatformFragPresenter(frag, this);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Info info = null;
        if (convertView == null) {
            convertView = inflater
                    .inflate(R.layout.market_plat_adapter, null);
            info = new Info();
            info.platAdapter_shareLayout = (RelativeLayout) convertView.findViewById(R.id.platAdapter_shareLayout);
            info.marketPlatAdapter_ticketNameTv = (TextView) convertView.findViewById(R.id.marketPlatAdapter_ticketNameTv);
            info.marketPlatAdapter_ticketUseTv = (TextView) convertView.findViewById(R.id.marketPlatAdapter_ticketUseTv);
            info.marketPlatAdapter_ticketUseTimeTv = (TextView) convertView.findViewById(R.id.marketPlatAdapter_ticketUseTimeTv);
            info.marketPlatAdapter_ticketPriceTv = (TextView) convertView.findViewById(R.id.marketPlatAdapter_ticketPriceTv);
            info.marketPlatAdapter_shareLayout = (LinearLayout) convertView.findViewById(R.id.marketPlatAdapter_shareLayout);
            convertView.setTag(info);
            AutoUtils.autoSize(convertView);
        } else {
            info = (Info) convertView.getTag();
        }
        info.platAdapter_shareLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share(position);
            }
        });
        info.marketPlatAdapter_ticketNameTv.setText(getItem(position).getName());
        info.marketPlatAdapter_ticketUseTv.setText(getItem(position).getReceiveRuleDesc());
        info.marketPlatAdapter_ticketUseTimeTv.setText("使用期限：" + getItem(position).getStartTime() + "-" + getItem(position).getEndTime());
        info.marketPlatAdapter_ticketPriceTv.setText(getItem(position).getConditionFee());
        if (getItem(position).isCanShare()) {
            info.marketPlatAdapter_shareLayout.setVisibility(View.VISIBLE);
        } else {
            info.marketPlatAdapter_shareLayout.setVisibility(View.GONE);
        }
        return convertView;
    }

    class Info {
        RelativeLayout platAdapter_shareLayout;
        TextView marketPlatAdapter_ticketNameTv;
        TextView marketPlatAdapter_ticketUseTv;
        TextView marketPlatAdapter_ticketUseTimeTv;
        TextView marketPlatAdapter_ticketPriceTv;
        LinearLayout marketPlatAdapter_shareLayout;
    }

    private void share(int position) {
        presenter.check(getItem(position).getId() + "", position);
//        ShareUtil.share(context, "https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-live-pusher.html", "11", "222");
    }


}
