package com.uiui.zyappstore.service.main;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.blankj.utilcode.util.NetworkUtils;
import com.trello.rxlifecycle4.LifecycleProvider;
import com.trello.rxlifecycle4.LifecycleTransformer;
import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import com.trello.rxlifecycle4.android.RxLifecycleAndroid;
import com.uiui.zyappstore.KeepAliveConnection;
import com.uiui.zyappstore.network.HTTPInterface;
import com.uiui.zyappstore.utils.JGYUtils;

import org.jetbrains.annotations.NotNull;

import java.io.File;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.subjects.BehaviorSubject;

public class MainService extends Service implements MainSContact.MainView, NetworkUtils.OnNetworkStatusChangedListener, LifecycleProvider<ActivityEvent> {
    private static final String TAG = MainService.class.getSimpleName();

    public MainSPresenter mPresenter;

    private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

    @Override
    public void onDisconnected() {
        Log.e(TAG, "网络未连接");
//        ToastUtil.show("网络未连接");
    }

    @Override
    public void onConnected(NetworkUtils.NetworkType networkType) {
        Log.e(TAG, "网络已连接");
//        ToastUtil.show("网络已连接");
        if (JGYUtils.getInstance().isScreenOn()) {
            HTTPInterface.checkUpdate(MainService.this);
            HTTPInterface.checkUpdate(MainService.this, "com.uiui.sn");
            mPresenter.getSnTag();
        }
    }

    @NotNull
    @Override
    public Observable<ActivityEvent> lifecycle() {
        return lifecycleSubject.hide();
    }

    @NotNull
    @Override
    public <T> LifecycleTransformer<T> bindUntilEvent(@NotNull ActivityEvent event) {
        return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
    }

    @NotNull
    @Override
    public <T> LifecycleTransformer<T> bindToLifecycle() {
        return RxLifecycleAndroid.bindActivity(lifecycleSubject);
    }

    public MainService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
//        throw new UnsupportedOperationException("Not yet implemented");
        return new KeepAliveConnection.Stub() {
        };
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        lifecycleSubject.onNext(ActivityEvent.CREATE);
        mPresenter = new MainSPresenter(this);
        mPresenter.setLifecycle(lifecycleSubject);
        mPresenter.attachView(this);
        NetworkUtils.registerNetworkStatusChangedListener(this);

        File file = new File(JGYUtils.getInstance().getDownLoadPath());
        if (file.isFile() || !file.exists()) {
            file.delete();
            file.mkdirs();
        }
        mPresenter.getSnTag();
        JGYUtils.getInstance().checkLocalExternalFiles();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
        NetworkUtils.unregisterNetworkStatusChangedListener(this);
        mPresenter.detachView();
    }

    @Override
    public void setSnTag() {

    }
}
