package com.sheep.gamegroup.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sheep.gamegroup.absBase.IApiRefresh;
import com.sheep.gamegroup.absBase.NetApiRefresh;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserFocusLog;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdpUserFocusLogList;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import org.afinal.simplecache.ApiKey;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import rx.functions.Action1;

import static com.sheep.gamegroup.absBase.AbsFocus.YES;

/**
 * Created by realicing on 2018/11/22.
 * realicing@sina.com
 * 小绵羊3.4.5新界面--我的关注--人员列表子模块
 */
public class FgtUserFocusLogList extends BaseListFragment6<String> {
    private List<String> tagList = new ArrayList<>();
    private AdpUserFocusLogList adpUserFocusLogList;
    private AdpUserFocusLogList adpUserFocusLogList2;

    private Action1<Integer> action1 = new Action1<Integer>() {
        @Override
        public void call(Integer integer) {
            switch (integer) {
                case YES:
                    refreshData();
                    break;
            }
        }
    };

    @Override
    protected void addApiRefresh(List<IApiRefresh> apiRefreshList) {
        tagList.add("1");
        tagList.add("2");
        NetApiRefresh<UserFocusLog> apiRefresh1 = new NetApiRefresh<UserFocusLog>(this) {
            @Override
            public String getKey(int page, int per_page) {
                return ApiKey.pageKeyUrl(ApiKey.getGameUserUserFocusList, page, per_page);
            }

            @Override
            public Observable<BaseMessage> getApi(ApiService apiService) {
                return apiService.getGameUserUserFocusList(page, per_page);
            }

            @Override
            public Class<UserFocusLog> getTClass() {
                return UserFocusLog.class;
            }
        };
        NetApiRefresh<UserFocusLog> apiRefresh2 = new NetApiRefresh<UserFocusLog>(this) {
            @Override
            public String getKey(int page, int per_page) {
                return ApiKey.pageKeyUrl(ApiKey.getGameUserUserFocusHotUser, page, per_page);
            }

            @Override
            public Observable<BaseMessage> getApi(ApiService apiService) {
                return apiService.getGameUserUserFocusHotUser(page, per_page);
            }

            @Override
            public Class<UserFocusLog> getTClass() {
                return UserFocusLog.class;
            }

            @Override
            protected void loadList(List<UserFocusLog> addList) {
                if (ListUtil.isEmpty(addList)) {
                    if (adpUserFocusLogList2.getHeaderLayoutCount() > 0) {
                        adpUserFocusLogList2.removeAllHeaderView();
                    }
                } else {
                    if (adpUserFocusLogList2.getHeaderLayoutCount() == 0) {
                        if (headerView == null)
                            headerView = LayoutInflater.from(SheepApp.getInstance()).inflate(R.layout.header_my_focus_hot_user, null);
                        adpUserFocusLogList2.addHeaderView(headerView);
                    }
                }
                super.loadList(addList);
            }
        };
        apiRefreshList.add(apiRefresh1);
        apiRefreshList.add(apiRefresh2);
        adpUserFocusLogList = new AdpUserFocusLogList(apiRefresh1.getList());
        adpUserFocusLogList2 = new AdpUserFocusLogList(apiRefresh2.getList());
        adpUserFocusLogList.setAction1(action1);
        adpUserFocusLogList2.setAction1(action1);
    }

    private View headerView;

    @Override
    protected BaseQuickAdapter<String, BaseViewHolder> getAdapter() {
        return new BaseQuickAdapter<String, BaseViewHolder>(R.layout.common_rv_wrap, tagList) {

            @Override
            protected void convert(BaseViewHolder holder, String item) {
                RecyclerView recyclerView = holder.itemView.findViewById(R.id.recyclerView);
                recyclerView.setHasFixedSize(true);
                recyclerView.setNestedScrollingEnabled(false);
                recyclerView.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
                recyclerView.setAdapter(TextUtils.equals(item, tagList.get(0)) ? adpUserFocusLogList : adpUserFocusLogList2);
            }
        };
    }

    /**
     * 设置关注按钮文本与状态
     *
     * @param focus
     * @param tv
     */
    public static void updateItemFocusTv(boolean focus, TextView tv) {
        if (focus) {
            ViewUtil.setText(tv, "已关注");
            tv.setBackgroundResource(R.drawable.selector_button_stroke_main);
            tv.setTextColor(SheepApp.getInstance().getResources().getColorStateList(R.color.selector_color_stoke_main_btn));
        } else {
            ViewUtil.setText(tv, "关注");
            tv.setBackgroundResource(R.drawable.selector_btn_stroke_333);
            tv.setTextColor(SheepApp.getInstance().getResources().getColorStateList(R.color.selector_color_stoke_333));
        }
    }
}
