package com.ruansee.macall.testwebview.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.rs.macall.androidx.basemodel.base.BaseViewBindActivity;
import com.rs.macall.androidx.basemodel.callback.PermissionListener;
import com.rs.macall.androidx.basemodel.utils.HandlerUtils;
import com.ruansee.macall.testwebview.app.RuanseeApp;
import com.ruansee.macall.testwebview.databinding.ActivityStartBinding;

import java.util.List;

public class StartActivity extends BaseViewBindActivity<ActivityStartBinding> implements PermissionListener {

    private Handler myHandler;

    private String[] permissionList = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.ACCESS_NETWORK_STATE};

    @Override
    protected ActivityStartBinding getViewBinding() {
        return ActivityStartBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        myHandler = HandlerUtils.getActivity(this);
    }

    @Override
    protected boolean isImmersiveStatusBar() {
        return true;
    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void start() {
        if (!this.isTaskRoot()) {
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
        viewBinding.tvMsg.setText("正在检测应用权限...");
        viewBinding.AVLoadingIndicatorView.smoothToShow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissionList, this);
        } else {
            initPage();
        }
    }

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

    private void initPage() {
        viewBinding.tvMsg.setText("权限通过，正在验证用户信息...");
        myHandler.sendEmptyMessageDelayed(1, 1000);
    }

    @Override
    public void handleMessage(Message msg) {
        if (msg.what == 1) {
            if (!RuanseeApp.getAppCacheData().isUserEffective()) {
                //无效
                viewBinding.tvMsg.setText("无效用户，请至设置用户信息...");
                myHandler.sendEmptyMessageDelayed(2, 1000);
            } else {
                viewBinding.tvMsg.setText("验证成功，正在跳转...");
                myHandler.sendEmptyMessageDelayed(3, 1000);
            }
        } else if (msg.what == 2) {
            viewBinding.AVLoadingIndicatorView.smoothToHide();
            Intent intent = new Intent(getContext(), SettingActivity.class);
            intent.putExtra("isSkipMain",true);
            startActivity(intent);
            finish();
        } else if (msg.what == 3) {
            viewBinding.AVLoadingIndicatorView.smoothToHide();
            startActivity(new Intent(getContext(), MainActivity.class));
            finish();
        }
    }

    @Override
    public void onGranted() {
        initPage();
    }

    @Override
    public void onDenied(List<String> deniDPermission) {
        showMissingPermissionDialog();
    }
}