package com.atmob.voiceai.module.subscription;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.media3.common.C;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.atmob.app.lib.base.BaseActivity;
import com.atmob.common.ui.SizeUtil;
import com.atmob.voiceai.data.api.bean.GoodsBean;
import com.atmob.voiceai.data.consts.EventId;
import com.atmob.voiceai.databinding.ActivitySubscriptionPageBinding;
import com.atmob.voiceai.dialog.CommonLoadingDialog;
import com.atmob.voiceai.dialog.SubRetentionDialog;
import com.atmob.voiceai.handlers.EventHandler;
import com.atmob.voiceai.utils.BoxingUtil;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class SubscriptionPageActivity extends BaseActivity<ActivitySubscriptionPageBinding> {


    private SubscriptionPageViewModel subscriptionPageViewModel;

    private int maxScrollY;
    private int minScrollY;

    private SubscriptionAdapter subscriptionAdapter;

    private SubRetentionDialog subRetentionDialog;

    private CommonLoadingDialog loadingDialog;

    public static void start(Context context) {
        Intent intent = new Intent(context, SubscriptionPageActivity.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();
    }

    private void initObserver() {
        subscriptionPageViewModel.getShowLoading().observe(this, data -> showLoadingDialog(BoxingUtil.boxing(data.getFirst()), data.getSecond()));
        subscriptionPageViewModel.getShowRetentionDialogEvent().observe(this, o -> showSubRetentionDialog());
        subscriptionPageViewModel.getFinishEvent().observe(this, o -> finish());
        subscriptionPageViewModel.getGoodsList().observe(this, list -> subscriptionAdapter.submitList(list));
    }

    private void initView() {
        initScrollView();
        initGoodsList();
    }


    public void showLoadingDialog(boolean show, String msg) {
        if (show) {
            if (loadingDialog == null) {
                loadingDialog = new CommonLoadingDialog(this);
            }
            loadingDialog.setMessage(msg);
            loadingDialog.show();
        } else if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }


    private void showSubRetentionDialog() {
        if (subRetentionDialog == null) {
            subRetentionDialog = new SubRetentionDialog(this);
            subRetentionDialog.setActionHandler(new SubRetentionDialog.ActionHandler() {
                @Override
                public void onSubRetentionClick() {
                    subscriptionPageViewModel.onSubRetentionClick();
                }

                @Override
                public void onCancelClick() {
                    finish();
                }
            });
            subRetentionDialog.setOnDismissListener(dialog -> EventHandler.report(EventId.retainmiss_001));
        }
        subscriptionPageViewModel.setRetentionTime();
        subRetentionDialog.show();
        EventHandler.report(EventId.retainstay_001);
    }

    private void initGoodsList() {
        subscriptionAdapter = new SubscriptionAdapter(this);
        binding.rvGoodsList.setAdapter(subscriptionAdapter);
        binding.rvGoodsList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        subscriptionAdapter.setActionHandler(goodsBean -> subscriptionPageViewModel.setCheckGoodsBean(goodsBean));
        binding.rvGoodsList.addItemDecoration(new RecyclerView.ItemDecoration() {

            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

                int childAdapterPosition = parent.getChildAdapterPosition(view);
                if (childAdapterPosition == subscriptionAdapter.getItemCount() - 1) {
                    outRect.set(0, 0, 0, 0);
                } else {
                    outRect.set(0, 0, 0, -(int) SizeUtil.dp2px(12.5f));
                }
            }
        });
    }

    private void initScrollView() {
        maxScrollY = (int) SizeUtil.dp2px(46);
        minScrollY = (int) SizeUtil.dp2px(20);
        binding.scrollView.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if (scrollY > maxScrollY) {
                binding.vStatusBar.setAlpha(1f);
                binding.vMenu.setAlpha(1f);
                binding.tvBarTitle.setAlpha(1f);
            } else if (scrollY <= minScrollY) {
                binding.vStatusBar.setAlpha(0f);
                binding.vMenu.setAlpha(0f);
                binding.tvBarTitle.setAlpha(0f);
            } else {
                float alpha = (scrollY - minScrollY) * 1f / (maxScrollY - minScrollY);
                binding.vStatusBar.setAlpha(alpha);
                binding.vMenu.setAlpha(alpha);
                binding.tvBarTitle.setAlpha(alpha);
            }
        });
    }


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

    @Override
    protected void initViewModel() {
        super.initViewModel();
        subscriptionPageViewModel = getViewModelProvider().get(SubscriptionPageViewModel.class);
        binding.setSubscriptionViewModel(subscriptionPageViewModel);
    }

    @Override
    public void onBackPressed() {
        if (!BoxingUtil.boxing(subscriptionPageViewModel.getIsShowCloseBtn().getValue())) {
            return;
        }
        subscriptionPageViewModel.onBackClick();
    }
}
