package com.swiftcleaner.chovey.view.activity;

import static com.zxdemo.ZxApplication.packname;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.cardview.widget.CardView;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.databinding.ActivityMainBinding;
import com.swiftcleaner.chovey.databinding.ActivitySettingBinding;
import com.swiftcleaner.chovey.view.dialog.CustomScoreDialog;
import com.zxdemo.ZxApplication;
import com.zxdemo.admob.AdmobHelper;
import com.zxdemo.http.ZxHttp;

public class SettingActivity extends AppCompatActivity {
    ActivitySettingBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        ZxHttp.INSTANCE.getHttpReportInterface("page_SettingActivity", "", null);
        initBar();
        initShow();
    }

    private void initShow() {
        binding.ivSettingReturn.setOnClickListener(v -> {
//            initCustDialog();
            finish();
        });
        binding.settingCl3Score.setOnClickListener(v -> {
            //评分弹窗
            showCustomDialog();
        });
        binding.settingCl3Policy.setOnClickListener(v -> {
            Uri uri = Uri.parse("https://sites.google.com/view/swift-cleaner/swift-clean");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        });
    }

    private void showCustomDialog() {
        Dialog dialog = new Dialog(this, R.style.TransparentDialogTheme);
        dialog.setContentView(R.layout.dialog_score_item);

        ImageView dialog_close = dialog.findViewById(R.id.dialog_close);
        AppCompatButton dialog_submit_button = dialog.findViewById(R.id.dialog_submit_button);
        CardView cardview = dialog.findViewById(R.id.cardview);
        cardview.setCardBackgroundColor(Color.TRANSPARENT);
        dialog_close.setOnClickListener(v -> {
            dialog.dismiss();
        });
        dialog_submit_button.setOnClickListener(v -> {
            dialog.dismiss();
            try {
                Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=" + packname);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                this.startActivity(intent);
            } catch (Exception e) {

            }
        });

        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    private void initBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()  // 透明状态栏
                .statusBarDarkFont(false)  // 状态栏字体颜色为浅色
                .navigationBarDarkIcon(true)  // 导航栏图标颜色为浅色
                .init();
    }

    @Override
    public void onBackPressed() {
//        initCustDialog();
        finish();
    }

    private void initCustDialog() {
        CustomScoreDialog customScoreDialog = new CustomScoreDialog(this);
        customScoreDialog.setOnDialogClickListener(new CustomScoreDialog.OnDialogClickListener() {
            @Override
            public void onCancel() {
                customScoreDialog.dismiss();
            }

            @Override
            public void onSubmit() {
                AdmobHelper.INSTANCE.showInterstitialAd(SettingActivity.this, () -> {
                            finish();
                            return null;
                        },
                        () -> {
                            return null;
                        },
                        () -> {
                            finish();
                            return null;
                        });
            }
        });
        customScoreDialog.show();
    }
}