package com.danding.yang800.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.danding.yang800.BaseActivity;
import com.danding.yang800.Entity.MarketInfo;
import com.danding.yang800.Entity.TicketNotUseInfo;
import com.danding.yang800.Fragment.MyTicketUsedFrag;
import com.danding.yang800.Presenter.MarketPlatformFragPresenter;
import com.danding.yang800.Presenter.MyTicketUsedFragPresenter;
import com.danding.yang800.R;
import com.danding.yang800.View.MyDialog;
import com.zhy.autolayout.utils.AutoUtils;

public class TicketUsedAdapter extends MyBaseAdapter<TicketNotUseInfo> {
    MyTicketUsedFragPresenter presenter;
    private MyTicketUsedFrag frag;
    BaseActivity context;

    public TicketUsedAdapter(BaseActivity context, MyTicketUsedFrag frag) {
        super(context);
        this.frag = frag;
        this.context = context;
        presenter = new MyTicketUsedFragPresenter(frag, this);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Info info = null;
        if (convertView == null) {
            convertView = inflater
                    .inflate(R.layout.ticket_used_adapter, null);
            info = new Info();
            info.ticketUsedAdapter_discountTv = (TextView) convertView.findViewById(R.id.ticketUsedAdapter_discountTv);
            info.ticketUsedAdapter_nameTv = (TextView) convertView.findViewById(R.id.ticketUsedAdapter_nameTv);
            info.ticketUsedAdapter_conditionTv = (TextView) convertView.findViewById(R.id.ticketUsedAdapter_conditionTv);
            info.ticketUsedAdapter_timeTv = (TextView) convertView.findViewById(R.id.ticketUsedAdapter_timeTv);
            info.ticketUsedAdapter_shareLayout = (LinearLayout) convertView.findViewById(R.id.ticketUsedAdapter_shareLayout);
            convertView.setTag(info);
            AutoUtils.autoSize(convertView);
        } else {
            info = (Info) convertView.getTag();
        }
        info.ticketUsedAdapter_discountTv.setText(getItem(position).getDiscountFee() + "");
        info.ticketUsedAdapter_nameTv.setText(getItem(position).getName());
        info.ticketUsedAdapter_conditionTv.setText(getItem(position).getCondition());
        info.ticketUsedAdapter_timeTv.setText("使用期限：" + getItem(position).getUserTimeRange());
        if (getItem(position).isCanShare()) {
            info.ticketUsedAdapter_shareLayout.setVisibility(View.VISIBLE);
        } else {
            info.ticketUsedAdapter_shareLayout.setVisibility(View.GONE);
        }
        info.ticketUsedAdapter_shareLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                share(position);
            }
        });
        return convertView;
    }

    private void share(int position) {
        presenter.check(getItem(position).getPromotionId() + "", position, getItem(position).getUserPromotionId());
    }

    class Info {
        TextView ticketUsedAdapter_discountTv;
        TextView ticketUsedAdapter_nameTv;
        TextView ticketUsedAdapter_conditionTv;
        TextView ticketUsedAdapter_timeTv;
        LinearLayout ticketUsedAdapter_shareLayout;
    }
}
