package com.datarecovery.master.module.imgrecover;

import static com.datarecovery.master.data.consts.EventId.EVENT_3000003;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.mediation.api.AdError;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.AdFuncId;
import com.datarecovery.master.data.consts.Constants;
import com.datarecovery.master.data.consts.EventId;
import com.datarecovery.master.databinding.ActivityImageRecoverBinding;
import com.datarecovery.master.databinding.ItemTabImageRecoverBinding;
import com.datarecovery.master.dialog.CommonLoadingDialog;
import com.datarecovery.master.dialog.CommonSureDialog;
import com.datarecovery.master.dialog.ScanFileDialog;
import com.datarecovery.master.dialog.UnlockFunctionDialog;
import com.datarecovery.master.handler.EventHelper;
import com.datarecovery.master.module.homepage.FunctionBean;
import com.datarecovery.master.module.member.MemberType;
import com.datarecovery.master.module.preview.PreviewActivity;
import com.datarecovery.master.sdk.ad.AtmobAdHelper;
import com.datarecovery.master.sdk.ad.RewardVideoListenerAdapter;
import com.datarecovery.master.utils.ActivityForResultUtil;
import com.datarecovery.master.utils.BoxingUtil;
import com.datarecovery.master.utils.CounterUtil;
import com.datarecovery.master.utils.FilePermissionHelper;
import com.datarecovery.master.utils.GridRecoverItemDecoration;
import com.datarecovery.master.utils.ImageDeepDetector;
import com.datarecovery.master.utils.Maps;
import com.datarecovery.master.utils.ToastUtil;
import com.google.android.material.tabs.TabLayout;
import com.gyf.immersionbar.ImmersionBar;

import java.util.Objects;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class ImageRecoverActivity extends BaseActivity<ActivityImageRecoverBinding> implements ImageItemAdapter.onItemClick {


    private static final String TYPE = "type";
    private final int[] tabTitle = {R.string.photo, R.string.wx, R.string.qq, R.string.other};
    ImageRecoverViewModel imageRecoverViewModel;
    private ConcatAdapter concatAdapter;
    private CommonSureDialog backDialog;
    private CommonSureDialog clearDialog;

    private CommonLoadingDialog loadingDialog;
    private ScanFileDialog scanFileDialog;
    private ImageItemAdapter photoAdapter;
    private ImageItemAdapter wxAdapter;
    private ImageItemAdapter qqAdapter;
    private ImageItemAdapter otherAdapter;
    private GridLayoutManager gridLayoutManager;
    private UnlockFunctionDialog unlockFunctionDialog;


    public static void start(Context context, @MemberType String type) {
        Intent intent = new Intent(context, ImageRecoverActivity.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();
        initPermission();
    }

    private void initPermission() {
        new FilePermissionHelper().requestDataFilePermission(this, new FilePermissionHelper.NextStepCallback() {
            @Override
            public void onNextStep() {
                imageRecoverViewModel.startImageScanning();
            }

            @Override
            public void onCancel() {
                EventHelper.report(EVENT_3000003, Maps.asMap("id", 106));
                finish();
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            imageRecoverViewModel.setType(intent.getStringExtra(TYPE));
        }
    }

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

    private void initRecycleView() {
        ImageTitleAdapter photoTitleAdapter = new ImageTitleAdapter(this, imageRecoverViewModel.getDetectedPhotoTitle());
        photoAdapter = new ImageItemAdapter(imageRecoverViewModel.getType(), this);
        photoAdapter.setOnItemClick(this);

        ImageTitleAdapter wxTitleAdapter = new ImageTitleAdapter(this, imageRecoverViewModel.getDetectedWxTitle());
        wxAdapter = new ImageItemAdapter(imageRecoverViewModel.getType(), this);
        wxAdapter.setOnItemClick(this);

        ImageTitleAdapter qqTitleAdapter = new ImageTitleAdapter(this, imageRecoverViewModel.getDetectedQQTitle());
        qqAdapter = new ImageItemAdapter(imageRecoverViewModel.getType(), this);
        qqAdapter.setOnItemClick(this);

        ImageTitleAdapter otherTitleAdapter = new ImageTitleAdapter(this, imageRecoverViewModel.getDetectedOtherTitle());
        otherAdapter = new ImageItemAdapter(imageRecoverViewModel.getType(), this);
        otherAdapter.setOnItemClick(this);

        concatAdapter = new ConcatAdapter(
                photoTitleAdapter,
                photoAdapter,
                wxTitleAdapter,
                wxAdapter,
                qqTitleAdapter,
                qqAdapter,
                otherTitleAdapter,
                otherAdapter
        );
        binding.ryImageRecover.setAdapter(concatAdapter);
        gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == getPhotoPosition() || position == getWxPosition() || position == getQQPosition() || position == getOtherPosition()) {
                    return 3;
                }
                return 1;
            }
        });
        binding.ryImageRecover.addItemDecoration(new GridRecoverItemDecoration(3, 0.0282222222222222f, 0.0202222222222222f));
        binding.ryImageRecover.setLayoutManager(gridLayoutManager);

        RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                int firstVisibleItem = gridLayoutManager.findFirstVisibleItemPosition();
                int lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
                int photoPosition = getPhotoPosition();
                int wxPosition = getWxPosition();
                int qqPosition = getQQPosition();
                int otherPosition = getOtherPosition();
                if (firstVisibleItem == photoPosition || (firstVisibleItem >= photoPosition && lastVisibleItem < wxPosition)) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(0));
                } else if (firstVisibleItem == wxPosition || (firstVisibleItem >= wxPosition && lastVisibleItem < qqPosition)) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(1));
                } else if (firstVisibleItem == qqPosition || (firstVisibleItem >= qqPosition && lastVisibleItem < otherPosition)) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(2));
                } else if (firstVisibleItem >= otherPosition) {
                    binding.tabLayout.selectTab(binding.tabLayout.getTabAt(3));
                }
            }
        };
        binding.ryImageRecover.addOnScrollListener(onScrollListener);
    }

    public int getPhotoPosition() {
        return 0;
    }

    public int getWxPosition() {
        return getPhotoPosition() + photoAdapter.getItemCount() + 1;
    }

    public int getQQPosition() {
        return getWxPosition() + wxAdapter.getItemCount() + 1;
    }

    public int getOtherPosition() {
        return getQQPosition() + qqAdapter.getItemCount() + 1;
    }

    @Override
    public void onCheck(ImageDeepDetector.ImageFile imageFile) {
        if (imageFile == null) {
            return;
        }
        imageRecoverViewModel.setItemCheck(imageFile);

        if (Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN)) {
        } else {
            EventHelper.report(EventId.EVENT_3000501);
        }
    }

    @Override
    public void onImgItemClick(ImageDeepDetector.ImageFile imageFile) {
        if (imageFile == null) {
            return;
        }
//        ToastUtil.show(imageFile.getPath(), ToastUtil.LENGTH_SHORT);
        if (Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN)) {
            imageRecoverViewModel.setItemCheck(imageFile);

            EventHelper.report(EventId.EVENT_3000900);
        } else {
            imageRecoverViewModel.checkPreview(imageFile);

            EventHelper.report(EventId.EVENT_3000500);
        }

    }

    private void initTabLayout() {
        binding.tabLayout.removeAllTabs();
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @SuppressLint("UseCompatLoadingForDrawables")
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getCustomView() == null) {
                    return;
                }
                ItemTabImageRecoverBinding itemBinding = ItemTabImageRecoverBinding.bind(tab.getCustomView());
                itemBinding.tabTitle.setTextColor(getResources().getColor(com.atmob.app.base.R.color.white));
                itemBinding.tabTitle.setBackground(getResources().getDrawable(R.drawable.bg_tab_selected));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.getCustomView() == null) {
                    return;
                }
                ItemTabImageRecoverBinding itemBinding = ItemTabImageRecoverBinding.bind(tab.getCustomView());
                itemBinding.tabTitle.setTextColor(getResources().getColor(R.color.tab_un_select_text_color));
                itemBinding.tabTitle.setBackground(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        for (int i = 0; i < tabTitle.length; i++) {
            TabLayout.Tab tab = binding.tabLayout.newTab();
            ItemTabImageRecoverBinding tabLayoutBinding = ItemTabImageRecoverBinding.inflate(getLayoutInflater());
            int finalI = i;
            tabLayoutBinding.getRoot().setOnClickListener(v -> {
                switch (finalI) {
                    case 0:
                        gridLayoutManager.scrollToPositionWithOffset(getPhotoPosition(), 0);
                        break;
                    case 1:
                        gridLayoutManager.scrollToPositionWithOffset(getWxPosition(), 0);
                        break;
                    case 2:
                        gridLayoutManager.scrollToPositionWithOffset(getQQPosition(), 0);
                        break;
                    case 3:
                        gridLayoutManager.scrollToPositionWithOffset(getOtherPosition(), 0);
                        break;
                }
                binding.tabLayout.selectTab(binding.tabLayout.getTabAt(finalI));
            });
            tabLayoutBinding.tabTitle.setText(tabTitle[i]);
            tab.setCustomView(tabLayoutBinding.getRoot());
            binding.tabLayout.addTab(tab);
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private void initObserver() {
        imageRecoverViewModel.getFailScrollPosition().observe(this, bean -> {
            if (bean == null) {
                return;
            }
            if (bean.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_GALLERY) {
                int position = photoAdapter.getPosition(bean);
                if (position != -1) {
                    gridLayoutManager.scrollToPositionWithOffset(getPhotoPosition() + position, 0);
                }
            } else if (bean.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_WECHAT) {
                int position = wxAdapter.getPosition(bean);
                if (position != -1) {
                    gridLayoutManager.scrollToPositionWithOffset(getWxPosition() + position, 0);
                }
            } else if (bean.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_QQ) {
                int position = qqAdapter.getPosition(bean);
                if (position != -1) {
                    gridLayoutManager.scrollToPositionWithOffset(getQQPosition() + position, 0);
                }
            } else if (bean.getCategory() == ImageDeepDetector.ImageFile.CATEGORY_OTHER) {
                int position = otherAdapter.getPosition(bean);
                if (position != -1) {
                    gridLayoutManager.scrollToPositionWithOffset(getOtherPosition() + position, 0);
                }
            }
        });
        imageRecoverViewModel.getScrollTop().observe(this, o -> binding.ryImageRecover.scrollToPosition(0));
        imageRecoverViewModel.getDeleteUriListSdk11().observe(this, list -> {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
                PendingIntent pi = MediaStore.createDeleteRequest(getBaseContext().getContentResolver(), list);
                IntentSenderRequest request = new IntentSenderRequest.Builder(pi.getIntentSender()).build();
                new ActivityForResultUtil<IntentSenderRequest, ActivityResult>().startActivityForResult(this, new ActivityResultContracts.StartIntentSenderForResult(), request, result -> {
                    imageRecoverViewModel.callDealComplete(result.getResultCode() == Activity.RESULT_OK);
                });
            }
        });
        imageRecoverViewModel.getNotifyList().observe(this, o -> {
            photoAdapter.notifyDataSetChanged();
            wxAdapter.notifyDataSetChanged();
            qqAdapter.notifyDataSetChanged();
            otherAdapter.notifyDataSetChanged();
        });
        imageRecoverViewModel.getShowScanDialogEvent().observe(this, this::showScanProgressDialog);
        imageRecoverViewModel.getDetectedPhotoImg().observe(this, list -> photoAdapter.submit(list));
        imageRecoverViewModel.getDetectedWxImg().observe(this, list -> wxAdapter.submit(list));
        imageRecoverViewModel.getDetectedQQImg().observe(this, list -> qqAdapter.submit(list));
        imageRecoverViewModel.getDetectedOtherImg().observe(this, list -> otherAdapter.submit(list));
        imageRecoverViewModel.getPreviewEvent().observe(this, pair -> PreviewActivity.startImagePreView(this, pair.first, pair.second));
        imageRecoverViewModel.getShowLoadingEvent().observe(this, this::showLoadingDialog);
        imageRecoverViewModel.getOnExportClickEvent().observe(this, o -> showUnlockDialog());
    }

    private void showUnlockDialog() {
        if (unlockFunctionDialog == null) {
            unlockFunctionDialog = new UnlockFunctionDialog(this, Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN) ? FunctionBean.IMG_CLEARING : FunctionBean.IMG_RECOVERY)
                    .setActionHandler(() -> {
                        if (Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN)) {
                            EventHelper.report(EventId.EVENT_3000901);
                            AtmobAdHelper.showVideo(AdFuncId.REWARD_CLEAN, new RewardVideoListenerAdapter() {
                                boolean isRewarded = false;

                                @Override
                                public void onRewarded(@androidx.annotation.NonNull String token) {
                                    super.onRewarded(token);
                                    isRewarded = true;
                                    showClearDialog();
                                }

                                @Override
                                public void onLoadFailed(AdError adError) {
                                    super.onLoadFailed(adError);
                                    showClearDialog();
                                }

                                @Override
                                public void onRewardAdShowFail(AdError adError) {
                                    super.onRewardAdShowFail(adError);
                                    showClearDialog();
                                }

                                @Override
                                public void onRewardAdClosed() {
                                    super.onRewardAdClosed();
                                    if (!isRewarded) {
                                        ToastUtil.show(R.string.clean_failed, ToastUtil.LENGTH_SHORT);
                                    }
                                }
                            });
                        } else {
                            AtmobAdHelper.showVideo(AdFuncId.REWARD_RECOVER, new RewardVideoListenerAdapter() {
                                boolean isRewarded = false;

                                @Override
                                public void onRewarded(@androidx.annotation.NonNull String token) {
                                    super.onRewarded(token);
                                    isRewarded = true;
                                    imageRecoverViewModel.exportImage();
                                }

                                @Override
                                public void onLoadFailed(AdError adError) {
                                    super.onLoadFailed(adError);
                                    imageRecoverViewModel.exportImage();
                                }

                                @Override
                                public void onRewardAdShowFail(AdError adError) {
                                    super.onRewardAdShowFail(adError);
                                    imageRecoverViewModel.exportImage();
                                }

                                @Override
                                public void onRewardAdClosed() {
                                    super.onRewardAdClosed();
                                    if (!isRewarded) {
                                        ToastUtil.show(R.string.recover_failed, ToastUtil.LENGTH_SHORT);
                                    }
                                }
                            });
                        }
                    });
        }
        unlockFunctionDialog.show();
    }

    private void showClearDialog() {
        if (clearDialog == null) {
            clearDialog = new CommonSureDialog(this);
            clearDialog.setDialogTitle(R.string.img_clear_sure_title).setDialogContent(R.string.img_clear_sure_content);
            clearDialog.setOnDialogClickListener(() -> imageRecoverViewModel.executeImageDelete());
        }
        clearDialog.show();
    }

    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.setOnShowListener(dialog ->
                    EventHelper.report(EventId.EVENT_3000200,
                            Maps.asMap("id", Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN) ? 105 : 101)));
        }
        backDialog.show();
    }

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

    public void showScanProgressDialog(Boolean show) {
        if (BoxingUtil.boxing(show)) {
            if (scanFileDialog == null) {
                scanFileDialog = new ScanFileDialog(this,
                        imageRecoverViewModel.getDetectedLastFileName(),
                        Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN) ? FunctionBean.IMG_CLEARING : FunctionBean.IMG_RECOVERY);
                scanFileDialog.setOnCancelListener(() -> imageRecoverViewModel.cancelScan());
            }
            scanFileDialog.show();
        } else {
            if (scanFileDialog != null) {
                scanFileDialog.dismiss();
            }
        }
    }

    public void showLoadingDialog(Boolean show) {
        if (BoxingUtil.boxing(show)) {
            if (loadingDialog == null) {
                loadingDialog = new CommonLoadingDialog(this);
                loadingDialog.setMessage(Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN) ? R.string.deleting : R.string.exporting);
            }
            loadingDialog.show();
        } else {
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (!CounterUtil.checkAndIncrementByDay(Constants.KEY_LIST_BACK_AD_COUNTER, 2)) {
            AtmobAdHelper.showInterstitial(AdFuncId.INTERSTITIAL_RESULT_BACK, null);
        }
        showBackDialog();
        EventHelper.report(EventId.EVENT_3000002,
                Maps.asMap("id", Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN) ? 105 : 101));
    }

    @Override
    public void finish() {
        super.finish();
        EventHelper.report(EVENT_3000003,
                Maps.asMap("id", Objects.equals(imageRecoverViewModel.getType(), MemberType.APP_IMAGE_CLEAN) ? 105 : 101));
    }

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


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

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

    @Override
    protected void initViewModel() {
        super.initViewModel();
        imageRecoverViewModel = getViewModelProvider().get(ImageRecoverViewModel.class);
        binding.setImageRecoverViewModel(imageRecoverViewModel);
    }

}
