package com.datarecovery.master.module.homepage;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

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

import com.atmob.app.lib.base.BaseFragment;
import com.atmob.common.ui.SizeUtil;
import com.atmob.mediation.api.AdError;
import com.datarecovery.master.R;
import com.datarecovery.master.data.consts.AdFuncId;
import com.datarecovery.master.databinding.FragmentHomePageBinding;
import com.datarecovery.master.dialog.CommonSureDialog;
import com.datarecovery.master.sdk.ad.AtmobAdHelper;
import com.datarecovery.master.sdk.ad.RewardVideoListenerAdapter;
import com.datarecovery.master.utils.PermissionUtil;
import com.datarecovery.master.utils.ToastUtil;
import com.datarecovery.master.utils.xfile.XFilePermission;
import com.datarecovery.master.widget.InformationSwitchBanner;
import com.gyf.immersionbar.ImmersionBar;

import dagger.hilt.android.AndroidEntryPoint;


@AndroidEntryPoint
public class HomePageFragment extends BaseFragment<FragmentHomePageBinding> {


    private HomePageViewModel homePageViewModel;

    private OtherFunctionAdapter otherFunctionAdapter;

    private CommonSureDialog requestManageDialog;
    private CommonSureDialog requestAndroidDataDialog;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initObserver();
    }

    private void initObserver() {
        homePageViewModel.getRequestManagePermission().observe(getViewLifecycleOwner(), o -> showManageDialog());
        homePageViewModel.getRequestAndroidDataPermission().observe(getViewLifecycleOwner(), o -> showAndroidDataDialog());
    }

    private void showManageDialog() {
        if (requestManageDialog == null) {
            requestManageDialog = new CommonSureDialog(requireContext());
            requestManageDialog.setDialogTitle(R.string.request_manage_permission_title)
                    .setDialogContent(R.string.request_manage_permission_content)
                    .setTitleGravity(Gravity.START)
                    .setContentGravity(Gravity.START)
                    .setSureText(R.string.teaching_limit)
                    .setOnDialogClickListener(this::requestManageFilePermission);
        }
        requestManageDialog.show();
    }

    private void showAndroidDataDialog() {
        if (requestAndroidDataDialog == null) {
            requestAndroidDataDialog = new CommonSureDialog(requireContext());
            requestAndroidDataDialog.setDialogTitle(R.string.request_android_data_permission_title)
                    .setDialogContent(R.string.request_android_data_permission_content)
                    .setCancelText(R.string.wait_moment)
                    .setSureText(R.string.teaching_limit)
                    .setOnDialogClickListener(this::requestAndroidDataPermission);

        }
        requestAndroidDataDialog.show();
    }

    private void requestAndroidDataPermission() {
        XFilePermission.requestAndroidDataPermission(getActivity(), null);
    }

    private void requestManageFilePermission() {
        PermissionUtil.requestWriteStoragePermission(getActivity(), null);
    }


    private void initView() {
        ViewGroup.LayoutParams layoutParams = binding.vInformationBg.getLayoutParams();
        if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
            ((ViewGroup.MarginLayoutParams) layoutParams).topMargin
                    = (int) (SizeUtil.getStatusBarHeight() + SizeUtil.dp2px(10));
        }
        initTextViewBanner();
        initHotFunction();
        initOtherFunction();
    }

    private void initHotFunction() {

    }

    private void initOtherFunction() {
        otherFunctionAdapter = new OtherFunctionAdapter(getViewLifecycleOwner(), homePageViewModel.getFunctionList());
        binding.ryOtherFunction.setAdapter(otherFunctionAdapter);
        binding.ryOtherFunction.setLayoutManager(new GridLayoutManager(requireContext(), 3));
        otherFunctionAdapter.setOnItemClick(bean -> AtmobAdHelper.showVideo(AdFuncId.REWARD_BEFORE_SCAN, new RewardVideoListenerAdapter() {
            boolean isRewarded = false;

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

            @Override
            public void onLoadFailed(AdError adError) {
                super.onLoadFailed(adError);
                homePageViewModel.clickItemFunction(bean);
            }

            @Override
            public void onRewardAdShowFail(AdError adError) {
                super.onRewardAdShowFail(adError);
                homePageViewModel.clickItemFunction(bean);
            }

            @Override
            public void onRewardAdClosed() {
                super.onRewardAdClosed();
                if (isRewarded) {
                    homePageViewModel.clickItemFunction(bean);
                } else {
                    ToastUtil.show(R.string.scan_failed, ToastUtil.LENGTH_SHORT);
                }
            }
        }));
    }

    private void initTextViewBanner() {
        InformationSwitchBanner mTextSwitchBanner = new InformationSwitchBanner(getContext(), binding.viewSwitcher);
        mTextSwitchBanner.update(homePageViewModel.getInformationList());
    }

    @Override
    protected void initViewModel() {
        super.initViewModel();
        homePageViewModel = getViewModelProvider().get(HomePageViewModel.class);
        binding.setHomePageViewModel(homePageViewModel);
    }

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