package com.sheep.gamegroup.view.fragment;


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

import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.api.OnItemClickListener;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.HomeListEntity;
import com.sheep.gamegroup.model.entity.SystemNotification;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.view.activity.ActMsg;
import com.sheep.gamegroup.view.adapter.MessageLeftAdapter;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.Locale;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/9/7.
 * realicing@sina.com
 */
public class FgtMsgCenter extends BaseListFragment2<SystemNotification> implements OnItemClickListener<SystemNotification> {

    @Override
    protected boolean isFirstGetACache() {
        return false;
    }

    @Override
    public int refreshDataType() {
        return 1;
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new MessageLeftAdapter(SheepApp.getInstance(), list, this);
    }

    @Override
    protected String getKey(int page, int per_page) {
        return String.format(Locale.CHINA, "app/message/system_notification?page=%d&per_page=%d&user_id=%s", page, per_page, DataUtil.getInstance().getUserId());
    }

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

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


    @Override
    public void notifyData() {
        if(activity instanceof ActMsg) {
            ((ActMsg) activity).updateMsgOval(0, lastMessage != null && lastMessage.getTotal() > 0);
        }
    }

    @Override
    public void onClickItem(View view, final SystemNotification item, int position) {
        if(item == null){
            return;
        }
        //注意：下面的接口调用不要删除，因为这里要去设置消息为可读
        SheepApp.getInstance().getNetComponent().getApiService().getSystemNotification(item.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                    }
                });
        switch (item.getType()){//消息类型 1:通知 2:活动 3:模块上线 4:任务上线 5:任务成功 6:任务失败
            case 3:
                try {
                    HomeListEntity homeListEntity = item.getRelation_data(HomeListEntity.class);
                    if(homeListEntity != null){
                        CommonUtil.getInstance().goWhere(activity, homeListEntity, "消息中心");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 4:
                Jump2View.getInstance().goTaskDetailView(activity, item.getRelation_id());
                break;
            case 5:
                Jump2View.getInstance().goAuditTastList(activity, FgtMainAudit.POSITION_SUCCESS);
                break;
            case 6:
                Jump2View.getInstance().goAuditTastList(activity, FgtMainAudit.POSITION_FAILED);
                break;
            case 1:
            case 2:
            default:
                Jump2View.getInstance().goActSysNfDetail(activity, item);
                break;
        }
    }
}
