package com.sheep.gamegroup.view.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.mdad.sdk.mdsdk.AdManager;
import com.mdad.sdk.mdsdk.GetAdListListener;
import com.mdad.sdk.mdsdk.common.AdData;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;

/**
 * Created by realicing on 2018/10/10.
 * realicing@sina.com
 */
public class FgtMiDongWeChatTaskList extends BaseListFragment<AdData> {
    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new RecyclerViewAdapter<AdData>(activity, R.layout.item_midong_wechat, list) {

            @Override
            public void convert(ViewHolder holder, final AdData item, int position) {
                if(item == null){
                    ViewUtil.setDefaultText((TextView) holder.itemView.findViewById(R.id.item_midong_wechat_name));
                    ViewUtil.setDefaultText((TextView) holder.itemView.findViewById(R.id.item_midong_wechat_price));
                    ViewUtil.setDefaultText((TextView) holder.itemView.findViewById(R.id.item_midong_wechat_des));
                    return;
                }
                ViewUtil.setText((TextView) holder.itemView.findViewById(R.id.item_midong_wechat_name), item.getName());
                ViewUtil.setText((TextView) holder.itemView.findViewById(R.id.item_midong_wechat_price), item.getPrice() + "元");
                ViewUtil.setText((TextView) holder.itemView.findViewById(R.id.item_midong_wechat_des), item.getDescription());
                Glide.with(activity).load(item.getLogo()).into((ImageView) holder.itemView.findViewById(R.id.item_midong_wechat_iv));
                holder.itemView.findViewById(R.id.item_midong_wechat_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AdManager.getInstance(SheepApp.getInstance()).openMiniProgram(activity, item);
                    }
                });
            }
        };
    }

    @Override
    public void initData() {
        LogUtil.println("FgtMiDongWeChatTaskList", "initData", "start");
        if(!ListUtil.isEmpty(list)){
            setNoMore(true);//一次性全部获取的，所以不需要翻页
            view_list.setNoMore(true);
            return;
        }
        AdManager.getInstance(SheepApp.getInstance()).getWeChatTaskList(activity, new GetAdListListener() {

            @Override
            public void onLoadAdSuccess(final List<AdData> newList) {
                LogUtil.println("FgtMiDongWeChatTaskList", "initData", "onLoadAdSuccess");
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ListUtil.addAll(list, newList);
                        notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onLoadAdFailure() {
                LogUtil.println("FgtMiDongWeChatTaskList", "initData", "onLoadAdFailure");
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged();
                    }
                });
            }

            @Override
            public void onAdEmpty() {
                LogUtil.println("FgtMiDongWeChatTaskList", "initData", "onAdEmpty");
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged();
                    }
                });
            }
        });
        LogUtil.println("FgtMiDongWeChatTaskList", "initData", "end");
    }
}
