package com.sheep.gamegroup.view.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.Applications;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserFocusGame;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.gamegroup.util.ApiUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import org.afinal.simplecache.ApiKey;

import io.reactivex.Observable;

/**
 * Created by realicing on 2018/11/22.
 * realicing@sina.com
 * 小绵羊3.4.5新界面--我的关注--游戏列表子模块
 */
public class FgtGameFocusList extends BaseListFragment2<UserFocusGame>{
    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new AdbCommonRecycler<UserFocusGame>(SheepApp.getInstance(), list) {
            @Override
            public int getViewIdByType(int type) {
                return R.layout.item_my_focus;
            }

            @Override
            public void convert(ViewHolder holder, final UserFocusGame  item) {
                ImageView item_my_focus_iv = holder.itemView.findViewById(R.id.item_my_focus_iv);
                TextView item_my_focus_info_tv = holder.itemView.findViewById(R.id.item_my_focus_info_tv);
                final TextView item_my_focus_tv = holder.itemView.findViewById(R.id.item_my_focus_tv);
                final Applications applications = item.getApplications();
                if(applications == null || applications.getId() <= 0) {
                    ViewUtil.setImage(item_my_focus_iv, "");
                    ViewUtil.setDefaultText(item_my_focus_info_tv);
                    ViewUtil.setDefaultText(item_my_focus_tv);
                    ViewUtil.setEnabled(item_my_focus_tv, false);
                } else {
                    ViewUtil.setGameImage(item_my_focus_iv, applications.getIcon());
                    ViewUtil.setText(item_my_focus_info_tv, applications.getName());
                    ViewUtil.setEnabled(item_my_focus_tv, true);
                    FgtUserFocusLogList.updateItemFocusTv(item.isFocus(), item_my_focus_tv);
                    item_my_focus_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(final View view) {
                            view.setEnabled(false);
                            ApiUtil.postGameUserFocusGame(item.getTheFocusId(), new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                                @Override
                                public void onNext(BaseMessage baseMessage) {
                                    G.showToast(item.isFocus() ? R.string.cancel_focus_success : R.string.focus_success);
                                    item.setFocus(!item.isFocus());
                                    FgtUserFocusLogList.updateItemFocusTv(item.isFocus(), item_my_focus_tv);
                                    view.setEnabled(true);
                                }

                                @Override
                                public void onError(BaseMessage baseMessage) {
                                    G.showToast(item.isFocus() ? R.string.cancel_focus_fail : R.string.focus_fail);
                                    view.setEnabled(true);
                                }
                            });
                        }
                    });
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Jump2View.getInstance().goPlayGameDetail(ActivityManager.getInstance().currentActivity(), item.getTheFocusId());
                        }
                    });

                }
            }
        };
    }

    @Override
    protected String getKey(int page, int per_page) {
        return ApiKey.pageKeyUrl(ApiKey.getGameUserGameFocusList, page, per_page);
    }

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

    @Override
    protected Class<UserFocusGame> getTClass() {
        return UserFocusGame.class;
    }
}
