package com.datarecovery.master.module.videorecover;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.atmob.app.lib.base.BaseActivity;
import com.datarecovery.master.R;
import com.datarecovery.master.databinding.ActivityVideoRecoverBinding;
import com.datarecovery.master.dialog.CommonLoadingDialog;
import com.datarecovery.master.dialog.CommonSureDialog;
import com.datarecovery.master.dialog.ScanProgressDialog;
import com.datarecovery.master.module.preview.PreviewActivity;
import com.datarecovery.master.utils.BoxingUtil;
import com.datarecovery.master.utils.FilePermissionHelper;
import com.datarecovery.master.utils.FilesSearch;
import com.datarecovery.master.utils.GridRecoverItemDecoration;
import com.gyf.immersionbar.ImmersionBar;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class VideoRecoverActivity extends BaseActivity<ActivityVideoRecoverBinding> {


    private VideoRecoverViewModel videoRecoverViewModel;
    private CommonLoadingDialog loadingDialog;
    private CommonSureDialog backDialog;
    private ScanProgressDialog scanProgressDialog;
    private VideoItemAdapter videoItemAdapter;


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

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

    private void initPermission() {
        new FilePermissionHelper().requestDataFilePermission(this, "hf11005", new FilePermissionHelper.NextStepCallback() {
            @Override
            public void onNextStep() {
                videoRecoverViewModel.startVideoScanning();
            }

            @Override
            public void onCancel() {
                finish();
            }
        });
    }

    private void initObserver() {
        videoRecoverViewModel.getShowLoadingEvent().observe(this, this::showLoadingDialog);
        videoRecoverViewModel.getDetectedVideoList().observe(this, list -> videoItemAdapter.submit(list));
        videoRecoverViewModel.getDetectedFinish().observe(this, o -> scanProgressDialog.detectedFinish());
        videoRecoverViewModel.getShowScanDialogEvent().observe(this, this::showScanProgressDialog);
    }

    private void initView() {
        binding.toolBar.setNavigationOnClickListener(v -> onBackPressed());
        addTopStatusBarHeight(binding.toolBar);
        initRecycleView();
    }

    private void initRecycleView() {
        videoItemAdapter = new VideoItemAdapter(this);
        binding.ryVideoRecover.setLayoutManager(new GridLayoutManager(this, 3));
        binding.ryVideoRecover.setAdapter(videoItemAdapter);
        binding.ryVideoRecover.addItemDecoration(new GridRecoverItemDecoration(3, 0.0282222222222222f, 0.0202222222222222f));
        videoItemAdapter.setOnItemClick(new VideoItemAdapter.onItemClick() {
            @Override
            public void onCheck(FilesSearch.DocumentFile file) {
                if (file == null) {
                    return;
                }
                videoRecoverViewModel.setItemCheck(file);
            }

            @Override
            public void onItemClick(FilesSearch.DocumentFile file) {
                PreviewActivity.startDocumentPreView(VideoRecoverActivity.this, PreviewActivity.TYPE_VIDEO, file);
            }
        });
    }

    public void showScanProgressDialog(Boolean show) {
        if (BoxingUtil.boxing(show)) {
            if (scanProgressDialog == null) {
                scanProgressDialog = new ScanProgressDialog(this, videoRecoverViewModel.getTotalDetectedCount());
                scanProgressDialog.setOnCancelListener(() -> videoRecoverViewModel.cancelScan());
            }
            scanProgressDialog.show();
        } else {
            if (scanProgressDialog != null) {
                scanProgressDialog.dismiss();
            }
        }
    }

    public void showLoadingDialog(Boolean show) {
        if (BoxingUtil.boxing(show)) {
            if (loadingDialog == null) {
                loadingDialog = new CommonLoadingDialog(this);
            }
            loadingDialog.show();
        } else {
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        }
    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }

    private void showBackDialog() {
        if (backDialog == null) {
            backDialog = new CommonSureDialog(this);
            backDialog.setDialogTitle(R.string.img_recovery_back_title)
                    .setDialogContent(R.string.img_recovery_back_content)
                    .setOnDialogClickListener(this::finish);
        }
        backDialog.show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        videoRecoverViewModel = getViewModelProvider().get(VideoRecoverViewModel.class);
        binding.setVideoRecoverViewModel(videoRecoverViewModel);
    }

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

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