package com.datarecovery.master.module.wxrecover;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.atmob.app.lib.base.BaseActivity;
import com.datarecovery.master.R;
import com.datarecovery.master.data.repositories.AccountRepository;
import com.datarecovery.master.databinding.ActivityWxRecoverBinding;
import com.datarecovery.master.dialog.PermissionDialog;
import com.datarecovery.master.module.login.LoginActivity;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.utils.FilePermissionHelper;
import com.gyf.immersionbar.ImmersionBar;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class WeChatRecoverActivity extends BaseActivity<ActivityWxRecoverBinding> {


    private WeChatViewModel weChatViewModel;
    private PermissionDialog loginDialog;


    public static void start(Context context, @MemberType String type) {
        Intent intent = new Intent(context, WeChatRecoverActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.putExtra("TYPE", type);
        context.startActivity(intent);
    }

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

    private void initData() {
        weChatViewModel.setType(getIntent().getStringExtra("TYPE"));
    }

    private void initView() {

    }

    private void showLoginDialog() {
        loginDialog = new PermissionDialog(this);
        loginDialog.setDialogContent(R.string.dialog_no_login)
                .setDialogTitle(R.string.dialog_no_login_title)
                .setOnDialogClickListener(new PermissionDialog.OnDialogClickListener() {
                    @Override
                    public void onClickSure() {
                        LoginActivity.start(WeChatRecoverActivity.this, weChatViewModel.getReportId());
                    }

                    @Override
                    public void onClickCancel() {
                        finish();
                    }
                });
        loginDialog.show();
    }

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

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

    @Override
    protected void initViewModel() {
        super.initViewModel();
        weChatViewModel = getViewModelProvider().get(WeChatViewModel.class);
        binding.setWechatViewModel(weChatViewModel);
    }

    @Override
    protected void configImmersion(@NonNull ImmersionBar immersionBar) {
        immersionBar.statusBarDarkFont(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (loginDialog != null && loginDialog.isShowing() && !TextUtils.isEmpty(AccountRepository.token)) {
            weChatViewModel.findOrder();
            loginDialog.dismiss();
        }
    }
}
