package com.swiftcleaner.chovey.view.activity;

import static com.swiftcleaner.chovey.GlobalConfig.URL_PRIVACY;

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

import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.swiftcleaner.chovey.GlobalConfig;
import com.swiftcleaner.chovey.R;
import com.swiftcleaner.chovey.business.EventUtils;
import com.swiftcleaner.chovey.business.admob.AdmobManager;
import com.swiftcleaner.chovey.databinding.ActivityMainBinding;
import com.swiftcleaner.chovey.databinding.ActivitySettingBinding;
import com.swiftcleaner.chovey.util.SPUtils;
import com.swiftcleaner.chovey.view.dialog.CustomScoreDialog;

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);
        EventUtils.INSTANCE.event("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(URL_PRIVACY);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                e.printStackTrace();
                Toast.makeText(this, "No app can handle this action", Toast.LENGTH_SHORT).show();
            }
        });

//        String token = SPUtils.Companion.getInstance().getString("token", "");
//        Log.d("setting_token",token);
//        binding.settingTitle.setOnClickListener(v->{
//            if (!token.isEmpty()){
//                binding.settingToken.setText(token);
//                binding.settingToken.setVisibility(View.VISIBLE);
//                copyTextToClipboard(token);
//            }else {
//                Toast.makeText(this, "token为空", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    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=" + GlobalConfig.PACKAGE_NAME);
                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() {
                AdmobManager.INSTANCE.showInterstitialAd(SettingActivity.this, () -> {
                            finish();
                            return null;
                        },
                        () -> {
                            return null;
                        },
                        () -> {
                            finish();
                            return null;
                        }, false);
            }
        });
        customScoreDialog.show();
    }

    //复制
    private void copyTextToClipboard(String text) {
        // 获取剪贴板管理器
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

        // 创建一个ClipData对象，包含要复制的文本
        ClipData clip = ClipData.newPlainText("simple text", text);

        // 将ClipData对象设置到剪贴板
        clipboard.setPrimaryClip(clip);

        // 可以在这里显示一个Toast消息，告知用户文本已复制
        Toast.makeText(this, "Text copied to clipboard", Toast.LENGTH_SHORT).show();
    }
}