package com.danding.yang800.Fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.danding.yang800.Adapter.TicketUsedAdapter;
import com.danding.yang800.MyTicketActivity;
import com.danding.yang800.Presenter.MyTicketUsedFragPresenter;
import com.danding.yang800.R;
import com.danding.yang800.Util.LogUtil;
import com.danding.yang800.Util.ShareUtil;
import com.danding.yang800.View.XListView;

@SuppressLint({"ValidFragment", "NewApi"})
public class MyTicketUsedFrag extends MyBaseFragment implements XListView.IXListViewListener {
    private MyTicketActivity context;
    XListView platform_frag_list;
    TicketUsedAdapter adapter;
    MyTicketUsedFragPresenter presenter;
    RelativeLayout platform_frag_emptyLayout;
    TextView platform_frag_emptyTv;

    public MyTicketUsedFrag(MyTicketActivity context) {
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.platform_frag, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        initData();
        super.onActivityCreated(savedInstanceState);
    }

    private void initData() {
        platform_frag_list = (XListView) getView().findViewById(R.id.platform_frag_list);
        platform_frag_emptyLayout = (RelativeLayout) getView().findViewById(R.id.platform_frag_emptyLayout);
        platform_frag_emptyTv = (TextView) getView().findViewById(R.id.platform_frag_emptyTv);
        platform_frag_emptyTv.setText("暂无相关券");
        platform_frag_list.setPullRefreshEnable(true);
        platform_frag_list.setPullLoadEnable(false);
        platform_frag_list.setAutoLoadEnable(false);
        platform_frag_list.setXListViewListener(this);
        adapter = new TicketUsedAdapter(context, this);
//        adapter.addData(new TicketNotUseInfo(1, 10, "1112", "ss", "455", true));
//        platform_frag_list.setAdapter(adapter);
        presenter = new MyTicketUsedFragPresenter(this, adapter);
        presenter.getInfo();
    }

    @Override
    public void onRefresh() {
        presenter.getInfo();
        context.reFresh();
    }

    @Override
    public void onLoadMore() {

    }

    public void updateData() {
        platform_frag_list.setAdapter(adapter);
        if (adapter.getLists().size() == 0) {
            platform_frag_emptyLayout.setVisibility(View.VISIBLE);
            platform_frag_list.setVisibility(View.GONE);
        } else {
            platform_frag_emptyLayout.setVisibility(View.GONE);
            platform_frag_list.setVisibility(View.VISIBLE);
        }
        onLoad();
    }

    public void onLoad() {
        platform_frag_list.stopRefresh();
        platform_frag_list.stopLoadMore();
    }

    public void share(int position, String result, String shopId, int userPromotionId) {
        ShareUtil.share(context, result + "?shopId=" + shopId + "&promotionId=" + adapter.getItem(position).getPromotionId() +
                        "&userPromotionId=" + userPromotionId,
                "土豪送你大红包啦！", "听说只有长的好看的人才能看到这个分享~", "", R.drawable.ticket_share);
        LogUtil.d(result + "?shopId=" + shopId + "&promotionId=" + adapter.getItem(position).getPromotionId() +
                "&userPromotionId=" + userPromotionId);

//        ClipboardManager cm;
//        cm = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
//        cm.setText(result + "?shopId=" + shopId + "&promotionId=" + adapter.getItem(position).getPromotionId() +
//                "&userPromotionId=" + userPromotionId);
    }
}
