package com.example.cuida.ui.sns24;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.example.cuida.databinding.FragmentSns24Binding;

public class Sns24Fragment extends Fragment {

    private FragmentSns24Binding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentSns24Binding.inflate(inflater, container, false);

        binding.buttonCallSns.setOnClickListener(v -> {
            try {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:808242424"));
                startActivity(intent);
            } catch (Exception e) {
                // In case no dialer app is found
                e.printStackTrace();
            }
        });

        binding.buttonTriage.setOnClickListener(v -> performTriage());

        return binding.getRoot();
    }

    private void performTriage() {
        boolean fever = binding.checkFever.isChecked();
        boolean breath = binding.checkBreath.isChecked();
        boolean pain = binding.checkPain.isChecked();

        if (breath || pain) {
            binding.textTriageResult.setText("URGENTE: Dirija-se imediatamente ao hospital ou ligue 112.");
            binding.textTriageResult.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
        } else if (fever) {
            binding.textTriageResult.setText("GRAVE: Ligue SNS 24 para aconselhamento antes de sair de casa.");
            binding.textTriageResult.setTextColor(getResources().getColor(android.R.color.holo_orange_dark));
        } else {
            binding.textTriageResult.setText("NÃO GRAVE: Fique em casa e monitorize os sintomas.");
            binding.textTriageResult.setTextColor(getResources().getColor(android.R.color.holo_green_dark));
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
