package com.sheep.gamegroup.util;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.absBase.IHomePageSearch;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.NewAboutUs;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.jiuyan.samllsheep.SheepApp;

import org.afinal.simplecache.ApiKey;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/6.
 * realicing@sina.com
 * 共用接口调用
 */
public class ApiUtil {

    //用户回复用户评论 点赞
    public static void postGameUserUserCommentLike(int user_comment_reply_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postGameUserUserCommentLike(user_comment_reply_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //用户游戏评论 表情点赞
    public static void postGameUserUserGameCommentExpressionLike(int user_comment_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postGameUserUserGameCommentExpressionLike(user_comment_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //用户游戏评论 点赞
    public static void postGameUserUserGameCommentLike(int user_comment_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postGameUserUserGameCommentLike(user_comment_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //用户游戏主页 点赞
    public static void postGameUserLike(int focus_user_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postGameUserLike(focus_user_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //用户游戏主页 关注
    public static void postGameUserFocusUser(int focus_user_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postGameUserFocusUser(focus_user_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //游戏 关注
    public static void postGameUserFocusGame(int focus_game_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postGameUserFocusGame(focus_game_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //视频 点赞
    public static void postVideoLike(int video_id, SheepSubscriber<BaseMessage> sheepSubscriber) {
        SheepApp.getInstance().getNetComponent().getApiService().postVideoLike(video_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(sheepSubscriber);
    }
    //视频 分享回调
    public static void postVideoShare(int video_id) {
        SheepApp.getInstance().getNetComponent().getApiService().postVideoShare(video_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbsObserver<BaseMessage>(){});
    }

    //关于我们，直接返回联系电话
    public static void getNewAboutUsComplainQq(final Action1<String> action1) {
        final NewAboutUs newAboutUs = DataUtil.getInstance().getCacheResult(ApiKey.new_about_us, NewAboutUs.class);
        if(newAboutUs != null && !TextUtils.isEmpty(newAboutUs.getComplaintQq())){
            Observable.just(newAboutUs.getComplaintQq())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new AbsObserver<String>() {
                        @Override
                        public void onNext(String qq) {
                            action1.call(qq);
                        }
                    });
            newAboutUs.setComplaintQq(null);//设置为空任务标记已经读取，下面的接口不用调用call
        }
        SheepApp.getInstance().getNetComponent().getApiService().getNewAboutUs()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                if(newAboutUs != null && TextUtils.equals(newAboutUs.getComplaintQq(), "hasRead")){
                                    return;
                                }
                                NewAboutUs item = baseMessage.getData(NewAboutUs.class);
                                if(item != null && !TextUtils.isEmpty(item.getComplaintQq())){
                                    action1.call(item.getComplaintQq());
                                } else {
                                    action1.call("2441310002");
                                }
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                if(newAboutUs != null && TextUtils.equals(newAboutUs.getComplaintQq(), "hasRead")){
                                    return;
                                }
                                action1.call("2441310002");
                            }
                        });
    }
    //点击热门搜索后，统计点击次数
    public static void postTopSearchStatisticsClickTopSearch(@Nullable IHomePageSearch item) {
        if(item != null)
            SheepApp.getInstance().getNetComponent().getApiService().postTopSearchStatisticsClickTopSearch(item.getLink_id(), item.getLink_type())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                            }
                        });
    }
}
