package com.danding.yang800.Fragment;

import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.danding.yang800.Adapter.MarketPlatAdapter;
import com.danding.yang800.MarketActivity;
import com.danding.yang800.MarketPlatFormInfoActivity;
import com.danding.yang800.Presenter.MarketPlatformFragPresenter;
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 MarketPlatformFrag extends MyBaseFragment implements XListView.IXListViewListener {
    private MarketActivity context;
    XListView platform_frag_list;
    MarketPlatAdapter adapter;
    MarketPlatformFragPresenter presenter;
    RelativeLayout platform_frag_emptyLayout;

    public MarketPlatformFrag(MarketActivity 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_list.setPullRefreshEnable(true);
        platform_frag_list.setPullLoadEnable(false);
        platform_frag_list.setAutoLoadEnable(false);
        platform_frag_list.setXListViewListener(this);
        platform_frag_list.setOnItemClickListener(listener);
        adapter = new MarketPlatAdapter(context, this);
        presenter = new MarketPlatformFragPresenter(this, adapter);
        presenter.getInfo();
    }

    android.widget.AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            Intent intent = new Intent(getActivity(), MarketPlatFormInfoActivity.class);
            intent.putExtra("id", adapter.getItem(position - 1).getId());
            getActivity().startActivity(intent);
        }
    };

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

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

    @Override
    public void onLoadMore() {

    }

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

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