package com.sheep.gamegroup.view.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kfzs.duanduan.fragment.FgtCreditCardProgressQuery;
import com.kfzs.duanduan.fragment.FgtCreditCardTaskList;
import com.kfzs.duanduan.mine.GiftpackListAdapter;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.CreditCardProgressQuery;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.AbsChooseImageActivity;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by realicing on 2018/4/24.
 */

public class ActCreditCardTaskList extends AbsChooseImageActivity {


    @BindView(R.id.indicator)
    TabLayout indicator;
    @BindView(R.id.pager)
    ViewPager pager;

    private GiftpackListAdapter mAdapter;

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

    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setTitle(this,"办卡赚钱")
                .setTitleFinish(this);

        mAdapter = new GiftpackListAdapter(getSupportFragmentManager(), getApplicationContext());
        mAdapter.add(new FgtCreditCardTaskList(), "任务列表");
        mAdapter.add(new FgtCreditCardProgressQuery(), "进度查询");
        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) {
                switch (i){
                    case 0:
                        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_CREDIT_CARD_ACCEPT_LIST);
                        break;
                    case 1:
                        UMConfigUtils.onEvent(UMConfigUtils.Event.SHEEP_CREDIT_CARD_PROGRESS_QUERY_LIST);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    public GiftpackListAdapter getmAdapter() {
        return mAdapter;
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onNotGetImage(String msg) {
        G.showToast(msg);
    }

    @Override
    protected void onGetImage(String path) {

    }
    private CreditCardProgressQuery creditCardProgressQuery;

    public void setCreditCardProgressQuery(CreditCardProgressQuery creditCardProgressQuery) {
        this.creditCardProgressQuery = creditCardProgressQuery;
    }

    @Override
    protected void onSetNetImageUrl(String url) {
        setLoaddingText("提交中");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("screenshots", url);
        releaseTaskCreditCard(creditCardProgressQuery.getId(), jsonObject);
    }

    @Override
    protected void onGetNetImageUrl(String url) {

    }

    public void releaseTaskCreditCardSuccess(BaseMessage baseMessage) {
        setLoaddingText("完成");
        dismissLoaddingDialog();
        G.showToast("提交成功，等待审核!");
        Fragment fragment = mAdapter.getItem(1);
        if(fragment instanceof FgtCreditCardProgressQuery){
            ((FgtCreditCardProgressQuery) fragment).initData();
        }
    }

    public void releaseTaskCreditCard(int id, JSONObject jsonObject) {
        SheepApp.getInstance().getNetComponent().getApiService().releaseTaskCreditCard(id, jsonObject)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.mContext) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        updateError(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        releaseTaskCreditCardSuccess(baseMessage);
                    }
                });
    }
}
