package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sheep.gamegroup.di.components.DaggerLieMakeMoneyComponent;
import com.sheep.gamegroup.di.modules.LieMakeMoneyModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.RecyleObj;
import com.sheep.gamegroup.model.entity.RecyleType;
import com.sheep.gamegroup.model.entity.WXOnHookAccount;
import com.sheep.gamegroup.presenter.LieMakeMoneyContract;
import com.sheep.gamegroup.presenter.LieMakeMoneyPresenter;
import com.sheep.gamegroup.view.adapter.LieMakeMoneyAdp;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by realicing on 2018/3/23.
 * realicing@sina.com
 */
public class LieMakeMoneyAct extends BaseActivity implements LieMakeMoneyContract.View{
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private Activity activity;
    private LieMakeMoneyAdp lieMakeMoneyAdp;
    @Inject
    LieMakeMoneyPresenter presenter;

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

    @Override
    public void initView() {
        activity = this;
        TitleBarUtils
                .getInstance()
                .setShowOrHide(this, true)
                .setTitle(activity, "躺着赚钱")
                .setTitleFinish(activity);
        DaggerLieMakeMoneyComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .lieMakeMoneyModule(new LieMakeMoneyModule(this))
                .build()
                .inject(this);
        recyclerview.setLayoutManager(new LinearLayoutManager(activity));
        lieMakeMoneyAdp = new LieMakeMoneyAdp(this);
        lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.START_ON_HOOK, null));
        lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.PROMPT_ON_HOOK, null));
        recyclerview.setAdapter(lieMakeMoneyAdp);
    }

    @Override
    public void initListener() {
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
                refresh.setEnabled(topRowVerticalPosition >= 0);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
    private boolean isFirst = true;
    @Override
    protected void onResume() {
        super.onResume();
        if(isFirst){
            isFirst = false;
            return;
        }
        setRefresh();
    }
    private void setRefresh(){
        if(refresh.isRefreshing()){
            return;
        }
        refresh.setRefreshing(true);
        initData();
    }
    @Override
    public void initData() {
        if(presenter == null){
            if(BuildConfig.DEBUG) G.showToast("请刷新项目重试");
            return;
        }
        presenter.getWebchat();
    }

    public static final int STATE_ON_HOOK_UNVERIFIED = 0;//未验证
    public static final int STATE_ON_HOOK_NORMAL = 1;//正常（脚本验证成功）
    public static final int STATE_ON_HOOK_NEED_VC = 2;//需要用户验证码
    public static final int STATE_ON_HOOK_BLOCKED = 3;//被封
    public static final int STATE_ON_HOOK_DEBLOCKED = 4;//用户已经解封等待服务器验证
    public static final int STATE_ON_HOOK_ABNORMAL = 5;//账号异常
    public static final int STATE_ON_HOOK_VC_OVERTIME = 6;//验证码超时请重新上传
    public static final int STATE_ON_HOOK_VERIFIED = 7;//用户已经验证等待脚本验证
    public static final int STATE_ON_HOOK_TOMORROW_RESET = 8;//三次验证码失败请明日再提交

    //{ "data": [ { "id": 14, "Uid": 12, "account": "trhhccg", "password": "", "verification_code": "", "status": 0, "remarks": "", "normal_time": 0, "create_time": 1521854985, "update_time": 0, "phone_number": "17628083502", "is_send": 0, "device_info": null } ] }
    @Override
    public void getWebchatUpdataView(BaseMessage baseMessage) {
        if(android.text.TextUtils.isEmpty(baseMessage.getData()+"")){
//            G.showToast("无数据!");
            return;
        }
        List<WXOnHookAccount> list = baseMessage.getDatas(WXOnHookAccount.class);
        lieMakeMoneyAdp.clear();
        for (WXOnHookAccount wxOnHookAccount : list) {
            switch (wxOnHookAccount.getStatus()) {
                case STATE_ON_HOOK_UNVERIFIED://未验证
                    lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.PROCESS_ON_HOOK, wxOnHookAccount));
                    break;
                case STATE_ON_HOOK_NORMAL://正常（脚本验证成功）
                    lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.NORMAL_ON_HOOK, wxOnHookAccount));
                    break;
                case STATE_ON_HOOK_BLOCKED:
                    lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.ABNORMAL_ON_HOOK, wxOnHookAccount));
                    break;
                case STATE_ON_HOOK_NEED_VC:
                case STATE_ON_HOOK_DEBLOCKED:
                case STATE_ON_HOOK_ABNORMAL:
                case STATE_ON_HOOK_VC_OVERTIME:
                case STATE_ON_HOOK_VERIFIED:
                case STATE_ON_HOOK_TOMORROW_RESET:
                default:
                    lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.ABNORMAL_ON_HOOK, wxOnHookAccount));
                    break;
            }
        }
        lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.PROMPT_ON_HOOK, list));
        lieMakeMoneyAdp.notifyDataSetChanged();
        refresh.setRefreshing(false);
    }

    @Override
    public void failGetWebchat2View(BaseMessage baseMessage) {
        G.showToast(baseMessage);
        lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.START_ON_HOOK, null));
        lieMakeMoneyAdp.add(RecyleObj.make(RecyleType.PROMPT_ON_HOOK, null));
        lieMakeMoneyAdp.notifyDataSetChanged();
        refresh.setRefreshing(false);
    }
}
