package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameAccountSuper;
import com.sheep.gamegroup.model.entity.RecyleObj;
import com.sheep.gamegroup.model.entity.RecyleType;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdpGameAccount;
import com.sheep.gamegroup.view.customview.SheepmSwipeRefreshLayout;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import io.reactivex.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_GAME_ACCOUNT_ADD;

/**
 * 游戏账号
 * Created by ljy on 2018/7/9.
 */

public class ActGameAccount extends BaseActivity {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refresh)
    SheepmSwipeRefreshLayout refresh;
    @BindView(R.id.empty_view)
    View empty_view;
    private Activity activity;

    private AdpGameAccount adpGameAccount;

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

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        activity = this;
        TitleBarUtils.getInstance()
                .setTitleFinish(activity)
                .setTitle(activity, "游戏账号")
                .setRightImgBotton(activity,
                        R.mipmap.add_black_img,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ViewUtil.newInstance().showAddAccount(activity, new Action1<Integer>() {
                                    @Override
                                    public void call(Integer integer) {
                                        initData();
                                    }
                                });
                                USER_GAME_ACCOUNT_ADD.onEvent();
                            }
                        });

        recyclerview.setLayoutManager(new LinearLayoutManager(activity));
        adpGameAccount = new AdpGameAccount(activity);
        for (int i = 0; i < 3; i++) {
            adpGameAccount.add(RecyleObj.make(RecyleType.NONE, null));
        }
        recyclerview.setAdapter(adpGameAccount);
    }

    @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);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                int topRowVerticalPosition =
                        (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();

                refresh.setEnabled(topRowVerticalPosition >= 0);
            }
        });
    }

    @Override
    public void initData() {
        adpGameAccount.clear();
        for (int i = 0; i < 3; i++) {
            adpGameAccount.add(RecyleObj.make(RecyleType.NONE, null));
        }
        SheepApp.getInstance()
                .getNetComponent()
                .getApiService()
                .getGameAccountAllList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {

                        G.showToast(baseMessage);
                        notifyDataSetChanged(null);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        GameAccountSuper gameAccountSuper = baseMessage.getData(GameAccountSuper.class);
                        if(gameAccountSuper != null){
                            int position = 0;
                            if(!ListUtil.isEmpty(gameAccountSuper.getPlatform())){
                                adpGameAccount.add(RecyleObj.make(RecyleType.GAME_ACCOUNT_PLATFORM, gameAccountSuper.getPlatform()),position);
                                position++;
                            }
                            if(!ListUtil.isEmpty(gameAccountSuper.getTengxun())){
                                adpGameAccount.add(RecyleObj.make(RecyleType.GAME_ACCOUNT_TENCENT, gameAccountSuper.getTengxun()),position);
                                position++;
                            }
                            if(!ListUtil.isEmpty(gameAccountSuper.getXiaomi())){
                                adpGameAccount.add(RecyleObj.make(RecyleType.GAME_ACCOUNT_XIAOMI, gameAccountSuper.getXiaomi()),position);
                            }

                        }
                        notifyDataSetChanged(gameAccountSuper);
                    }
                });
    }

    private void checkAndInitView() {
        if(refresh == null)
            refresh = findViewById(R.id.refresh);
        if(empty_view == null)
            empty_view = findViewById(R.id.empty_view);
    }
    private void notifyDataSetChanged(GameAccountSuper gameAccountSuper){
        checkAndInitView();
        adpGameAccount.notifyDataSetChanged();
        refresh.setRefreshing(false);
        if(gameAccountSuper == null){
            CommonUtil.getInstance().updateEmptyView(empty_view, true);
            return;
        }
        if(ListUtil.isEmpty(gameAccountSuper.getPlatform()) && ListUtil.isEmpty(gameAccountSuper.getTengxun()) && ListUtil.isEmpty(gameAccountSuper.getXiaomi())){
            CommonUtil.getInstance().updateEmptyView(empty_view, true);
        }else {
            empty_view.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void onEventMainThread(BigEvent event){
        switch (event.getEventTypes()){
            case DELETE_GAME_ACCOUNT_REFRESH:
                initData();
                break;
        }
    }
}
