package kr.co.zumo.app.lifeplus.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import kr.co.zumo.app.R;
import kr.co.zumo.app.lifeplus.application.App;
import kr.co.zumo.app.lifeplus.model.SuperModel;
import kr.co.zumo.app.lifeplus.model.SuperModelInit;
import kr.co.zumo.app.lifeplus.network.NetworkWatcher;
import kr.co.zumo.app.lifeplus.supervisor.DialogHelper;
import kr.co.zumo.app.lifeplus.util.AppUtil;
import kr.co.zumo.app.lifeplus.util.PermissionUtil;

/**
 * Splash 화면
 * <pre>
 * Splash 화면, manifests 에서 설정한 Theme 로 splash 화면이 구성된다.
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018-09-07]   [최초 작성]
 */
public class SplashActivity extends AppCompatActivity {

  CompositeDisposable disposable = new CompositeDisposable();
  ActivityStarter starter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DialogHelper.getInstance().setFragmentManager(getSupportFragmentManager());
    Context context = App.getInstance().getContext();
    SuperModelInit.init(context);

    setDeviceId(AppUtil.getDeviceId(context));

    starter = new ActivityStarter(this);

    disposable.add(NetworkWatcher.check(context)
      .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
      .subscribe(isConnected -> {
        disposable.dispose();

        starter.launch(isConnected);
      }, Throwable::printStackTrace)
    );
  }

  private void setDeviceId(String deviceId) {
    Log.w("APP# SplashActivity | setDeviceId", "| " + deviceId);
    SuperModel.getInstance().setDeviceId(deviceId);
  }

  @Override
  public void startActivity(Intent intent) {
    super.startActivity(intent);
    overridePendingTransition(R.anim.none, R.anim.none);
  }

  protected void requestPermissions() {
    PermissionUtil.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 99);
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
      case 1001: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          // permission was granted, yay! Do the
        }
        else {
          // permission denied, boo! Disable the
        }
        break;
      }
      default:
        break;
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();

    if (null != starter) {
      starter.dispose();
      starter = null;
    }
    if (null != disposable) {
      disposable.dispose();
      disposable = null;
    }
  }
}

