package com.sheep.gamegroup.view.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserFocusLog;
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.ViewUtil;
import com.sheep.gamegroup.view.fragment.FgtUserFocusLogList;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;

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 AdpUserFocusLogList extends BaseQuickAdapter<UserFocusLog, BaseViewHolder> {
    public AdpUserFocusLogList(List<UserFocusLog> list) {
        super(R.layout.item_my_focus, list);
    }

    private Action1<Integer> action1;

    public void setAction1(Action1<Integer> action1) {
        this.action1 = action1;
    }

    @Override
    public void convert(BaseViewHolder holder, final UserFocusLog  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);
        if(item == null || item.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.setAvatar(item_my_focus_iv, item.getAvatar());
            ViewUtil.setText(item_my_focus_info_tv, item.getInfo());
            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.postGameUserFocusUser(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);
                            if(action1 != null)
                                action1.call(YES);
                            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().goActUserAppHome(ActivityManager.getInstance().currentActivity(), item.getTheFocusId());
                }
            });

        }
    }
}
