package com.datarecovery.master.module.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.common.runtime.ContextUtil;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.databinding.ActivityLoginBinding;
import com.datarecovery.master.module.browser.BrowserActivity;
import com.datarecovery.master.utils.SpannableUtil;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class LoginActivity extends BaseActivity<ActivityLoginBinding> {


    private static final String REPORT_ID = "reportId";
    private LoginViewModel loginViewModel;


    public static void start(Context context, String reportId) {
        Intent intent = new Intent(context, LoginActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra(REPORT_ID, reportId);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        initObserver();
    }

    private void initData() {
        loginViewModel.setReportId(getIntent().getStringExtra(REPORT_ID));
    }

    private void initView() {
        String agreeText = getString(R.string.login_agree_text);
        String userTermsText = getString(R.string.login_agree_user_terms_text);
        String privacyPolicyText = getString(R.string.login_agree_privacy_policy_text);
        SpannableUtil.getAgreementSpannableStringBuilder(binding.loginAgreeText, agreeText, new String[]{userTermsText, privacyPolicyText}, getResources().getColor(R.color.colorPrimary), false,
                v -> BrowserActivity.start(getBaseContext(), Constants.PRIVACY_POLICY), v -> BrowserActivity.start(getBaseContext(), Constants.USER_AGREEMENT));
    }

    private void initObserver() {
        loginViewModel.getFinishEvent().observe(this, o -> finish());
    }

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

    @Override
    protected void initViewModel() {
        super.initViewModel();
        loginViewModel = getViewModelProvider().get(LoginViewModel.class);
        binding.setLoginViewModel(loginViewModel);
    }
}
