package com.tengshisoft.mainmodule.base;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.tengshisoft.mainmodule.R;
import com.tenlionsoft.baselib.constant.LionActions;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.constant.PermissionConstants;
import com.tenlionsoft.baselib.core.widget.base.BaseActivity;
import com.tenlionsoft.baselib.utils.PermissionUtils;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

@Route(path = PathConfig.PATH_MODULE_MAIN_SPLASH)
public class SplashActivity extends BaseActivity {

    private Unbinder mBind;
    private ImmersionBar mImmersionBar;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void doShowOptions() {

    }

    @Override
    protected void clearSearch() {

    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    public void initData() {
        mBind = ButterKnife.bind(this);
        refreshView(STATE_LOAD_SUCCESS);
        ImmersionBar.with(this).init();
        mRlTitleBar.setVisibility(View.GONE);

        PermissionUtils.permission(PermissionConstants.STORAGE,
            PermissionConstants.CAMERA,
            PermissionConstants.MICROPHONE)
            .callback(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {
                    Observable.timer(300, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<Long>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(Long aLong) {
                                ARouter.getInstance()
                                    .build(PathConfig.PATH_MODULE_MAIN_LOGIN)
                                    .withString(LionActions.EXTRA_PUSH_MSG, getIntent().getStringExtra(LionActions.EXTRA_PUSH_MSG))
                                    .withInt(LionActions.EXTRA_MSG_TYPE, getIntent().getIntExtra(LionActions.EXTRA_MSG_TYPE, -1))
                                    .withString("to", getIntent().getStringExtra("to"))
                                    .withString("time", getIntent().getStringExtra("time"))
                                    .withString(LionActions.EXTRA_MSG_CONF_ID, getIntent().getStringExtra(LionActions.EXTRA_MSG_CONF_ID))
                                    .navigation();
                                finish();
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                }

                @Override
                public void onDenied() {
                    ToastUtils.show("App需要读取SD的权限,请前往设置里授权.");
                    ARouter.getInstance()
                        .build(PathConfig.PATH_MODULE_MAIN_LOGIN)
                        .withString(LionActions.EXTRA_PUSH_MSG, getIntent().getStringExtra(LionActions.EXTRA_PUSH_MSG))
                        .withInt(LionActions.EXTRA_MSG_TYPE, getIntent().getIntExtra(LionActions.EXTRA_MSG_TYPE, -1))
                        .withString(LionActions.EXTRA_MSG_CONF_ID, getIntent().getStringExtra(LionActions.EXTRA_MSG_CONF_ID))
                        .withString("to", getIntent().getStringExtra("to"))
                        .withString("time", getIntent().getStringExtra("time"))
                        .navigation();
                    finish();
                }
            }).request();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            mBind.unbind();
        }
    }
}
