package com.sheep.gamegroup.view.activity;

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

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.view.fragment.FgtCreditCardProgressQuery;
import com.sheep.gamegroup.view.fragment.FgtCreditCardTaskList;
import com.sheep.gamegroup.view.adapter.TitleFragmentListAdapter;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.CreditCardProgressQuery;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewUtil;
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 io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.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 TitleFragmentListAdapter mAdapter;

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

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

        mAdapter = new TitleFragmentListAdapter(getSupportFragmentManager());
        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 TitleFragmentListAdapter 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.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        updateError(baseMessage.getErrorMsg());
                    }

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