package com.auto.clicker.autoclicker.ui.activity.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.auto.clicker.autoclicker.R;
import com.auto.clicker.autoclicker.databinding.ActivityAgreementBinding;
import com.auto.clicker.autoclicker.ui.activity.main.MainActivity;

public class AgreementActivity extends AppCompatActivity {
    private ActivityAgreementBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityAgreementBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initData();
        initEvent();
    }

    private void initData() {
        binding.textAgreement.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void initEvent() {
        binding.btnStart.setOnClickListener(v -> {
            if (binding.checkBoxAgree.isChecked()) {
                setAgreed();

                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else {
                Toast.makeText(this, "Please first check the box to agree to the privacy policy and terms of use", Toast.LENGTH_SHORT).show();
            }
        });

        binding.textAgreement.setMovementMethod(LinkMovementMethod.getInstance());
    }

    private void setAgreed() {
        getSharedPreferences("app_prefs", MODE_PRIVATE)
                .edit()
                .putBoolean("agreed_privacy", true)
                .apply();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}
