package com.hainaos.vc.activity.splash;

import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.databinding.DataBindingUtil;

import com.hainaos.vc.R;
import com.hainaos.vc.activity.main.MainActivity;
import com.hainaos.vc.base.BaseDataBindingActivity;
import com.hainaos.vc.config.CommonConfig;
import com.hainaos.vc.databinding.ActivitySplashBinding;
import com.hainaos.vc.dialog.PrivacyPolicyDialog;
import com.tencent.mmkv.MMKV;

public class SplashActivity extends BaseDataBindingActivity {
    private static final String TAG = "SplashActivity";

    private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);

    private ActivitySplashBinding mBinding;

    @Override
    protected void initDataBinding() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_splash);
    }

    @Override
    protected void initView() {
        boolean showed = mMMKV.decodeBool(CommonConfig.SHOW_SPLASH_KEY, false);
        Log.e(TAG, "initView: showed = " + showed);
        boolean agree = mMMKV.decodeBool(CommonConfig.AGREED_THE_PRIVACY_AGREEMENT, false);
        Log.e(TAG, "initView: agree = " + agree);
        if (showed && agree) {
            openMain();
        } else {
            showPolicyDialog();
        }
    }

    @Override
    protected void initData() {

    }

    private PrivacyPolicyDialog mPrivacyPolicyDialog;

    private void showPolicyDialog() {
        if (mPrivacyPolicyDialog == null) {
            mPrivacyPolicyDialog = new PrivacyPolicyDialog(this);
            mPrivacyPolicyDialog.setCancelable(false);
            mPrivacyPolicyDialog.setPrivacyPolicyCallback(new PrivacyPolicyDialog.PrivacyPolicyCallback() {
                @Override
                public void onCancel() {
                    mPrivacyPolicyDialog.dismiss();
                    mPrivacyPolicyDialog = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        finishAndRemoveTask();
                    } else {
                        finishAffinity();
                    }
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);
                }

                @Override
                public void onConfirm() {
                    mMMKV.encode(CommonConfig.SHOW_SPLASH_KEY, true);
                    mMMKV.encode(CommonConfig.AGREED_THE_PRIVACY_AGREEMENT, true);
                    mPrivacyPolicyDialog.dismiss();
                    mPrivacyPolicyDialog = null;
                    openMain();
                }
            });
        }
        if (!mPrivacyPolicyDialog.isShowing()) {
            mPrivacyPolicyDialog.show();
        }
    }

    private void openMain() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    public class BtnClick {

    }
}
