package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.mdad.sdk.mdsdk.AdManager;
import com.mdad.sdk.mdsdk.AppDownloadListener;
import com.mdad.sdk.mdsdk.RewardListener;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
import com.sheep.gamegroup.view.fragment.BaseListFragment;
import com.sheep.gamegroup.view.fragment.FgtMiDongAdTskList;
import com.sheep.gamegroup.view.fragment.FgtMiDongSignTskList;
import com.sheep.gamegroup.view.fragment.FgtMiDongWeChatTaskList;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.afinal.simplecache.ACache;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;

/**
 * 幂动科技 任务
 * Created by ljy on 2018/7/19.
 */

public class ActMiDong extends BaseActivity {
    @BindView(R.id.indicator)
    TabLayout indicator;
    @BindView(R.id.pager)
    ViewPager pager;
    private TitleFragmentListAdapter mAdapter;

    private Activity activity;

    @Override
    protected int getLayoutId() {
        return R.layout.common_tab_vp;
    }

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils.getInstance()
                .setShowOrHide(this, true)
                .setTitle(activity, "简单奖励")
                .setTitleFinish(activity)
                .setRightImgBotton(activity, R.mipmap.question_black_img, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ViewUtil.showMsgDialog(activity, new DialogConfig().setTitle("温馨提示").setBtnLeftText("我知道了")
                                .setMsg("该栏目为现金任务，不参与好友提成。"));
                    }
                });
    }

    @Override
    public void initListener() {

    }
    private int curPosition = 0;
    private List<BaseFragment> fgtMiDongList = ListUtil.emptyList();
    @Override
    public void initData() {
        mAdapter = new TitleFragmentListAdapter(getSupportFragmentManager());
        fgtMiDongList.add(new FgtMiDongAdTskList());
        fgtMiDongList.add(new FgtMiDongSignTskList());
        fgtMiDongList.add(new FgtMiDongWeChatTaskList());
        mAdapter.add(fgtMiDongList.get(0), "普通奖励");
        mAdapter.add(fgtMiDongList.get(1), "签到奖励");
        mAdapter.add(fgtMiDongList.get(2), "小程序奖励");
        pager.setAdapter(mAdapter);
        indicator.setupWithViewPager(pager);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                curPosition = i;
//                FIND_TAG.onEvent("find_tag_name", ListUtil.hasIndex(list, i - 1) ? list.get(i - 1).getName() : "全部");
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        pager.setCurrentItem(0);
        pager.setOffscreenPageLimit(fgtMiDongList.size());
        CommonUtil.getInstance().reflex(indicator, activity);
        AdManager.getInstance(SheepApp.getInstance()).setDownloadListener(new AppDownloadListener() {
            @Override
            public void onDownloadStart(String id) {
                LogUtil.println("AdManager", "onDownloadStart", id);
                ACache.get(SheepApp.getInstance()).put(String.format(Locale.CHINA, "%s%s", AdManager.class.getSimpleName(), id), "onDownloadStart");
                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.REFRESH_MING_DONG));

            }

            @Override
            public void onDownloadFail(String id) {
                LogUtil.println("AdManager","onDownloadFail", id);
                ACache.get(SheepApp.getInstance()).put(String.format(Locale.CHINA, "%s%s", AdManager.class.getSimpleName(), id), "onDownloadFail");
                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.REFRESH_MING_DONG));

            }

            @Override
            public void onDownloadSuccess(String id) {//没有收到回调
                LogUtil.println("AdManager","onDownloadSuccess", id);
                ACache.get(SheepApp.getInstance()).put(String.format(Locale.CHINA, "%s%s", AdManager.class.getSimpleName(), id), "onDownloadSuccess");
                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.REFRESH_MING_DONG));

            }
        });
        AdManager.getInstance(SheepApp.getInstance()).setRewardListener(new RewardListener() {
            @Override
            public void doTaskSuccess(String s) {
                LogUtil.println("AdManager","onDownloadFail", s);
                ACache.get(SheepApp.getInstance()).put(String.format(Locale.CHINA, "%s%s", AdManager.class.getSimpleName(), s), "doTaskSuccess");
                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.REFRESH_MING_DONG));

            }

            @Override
            public void doTaskFail(String s) {
                LogUtil.println("AdManager","onDownloadFail", s);
                ACache.get(SheepApp.getInstance()).put(String.format(Locale.CHINA, "%s%s", AdManager.class.getSimpleName(), s), "doTaskFail");
                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.REFRESH_MING_DONG));

            }
        });
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!EventBus.getDefault().isRegistered(this))
             EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(BigEvent event){
        switch (event.getEventTypes()){
            case REFRESH_MING_DONG:
                BaseFragment item = ListUtil.getItem(fgtMiDongList, curPosition);
                if(item instanceof FgtMiDongAdTskList || item instanceof FgtMiDongSignTskList){
                    ((BaseListFragment) item).refreshData();
                }
                break;
        }
    }
}
