package com.example.punyaria;

import androidx.appcompat.app.AppCompatActivity;
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 android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HotlineActivity extends AppCompatActivity {

    private ListView listView;

    private String[] hotlineItems = {"WhatsApp", "Telepon", "Instagram", "Email"};
    private String[] hotlineSubItems = {"081138808800", "081138808800", "uptd_ppajember21", "pusatpelayananterpadu@gmail.com"};
    private int[] hotlineIcons = {R.drawable.whatsapp, R.drawable.wa, R.drawable.instagram, R.drawable.email}; // Sesuaikan dengan sumber gambar yang benar

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hotline);
        getSupportActionBar().hide();

        ImageView headerImageView = findViewById(R.id.headerImageView);
        headerImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        listView = findViewById(R.id.hotline);

        // Membuat adapter kustom untuk ListView
        HotlineAdapter adapter = new HotlineAdapter(hotlineItems, hotlineSubItems, hotlineIcons);
        listView.setAdapter(adapter);

        // Menangani klik item ListView
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = hotlineItems[position];
                String selectedSubItem = hotlineSubItems[position];

                // Membuat intent untuk membuka layanan WhatsApp
                if (selectedItem.equals("WhatsApp")) {
                    String url = "https://api.whatsapp.com/send?phone=" + +62 + selectedSubItem;
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                }
                // Membuat intent untuk melakukan panggilan telepon
                else if (selectedItem.equals("Telepon")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + selectedSubItem));
                    startActivity(intent);
                }
                // Membuat intent untuk membuka aplikasi Instagram
                else if (selectedItem.equals("Instagram")) {
                    String url = "https://www.instagram.com/" + selectedSubItem;
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                }
                // Membuat intent untuk membuka aplikasi email
                else if (selectedItem.equals("Email")) {
                    Intent intent = new Intent(Intent.ACTION_SENDTO);
                    intent.setData(Uri.parse("mailto:"));
                    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{selectedSubItem});
                    startActivity(Intent.createChooser(intent, "Choose an Email client :"));
                }
                // Tambahkan logika lainnya sesuai kebutuhan
                else {
                    Toast.makeText(HotlineActivity.this, "Anda memilih: " + selectedItem, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // Kelas adapter kustom untuk menampilkan item hotline dengan ikon dan subjudul
    private class HotlineAdapter extends ArrayAdapter<String> {
        private final String[] items;
        private final String[] subItems;
        private final int[] icons;

        public HotlineAdapter(String[] items, String[] subItems, int[] icons) {
            super(HotlineActivity.this, R.layout.list_hotline, items);
            this.items = items;
            this.subItems = subItems;
            this.icons = icons;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.list_hotline, parent, false);

            TextView textViewTitle = rowView.findViewById(R.id.menu_title);
            TextView textViewSubTitle = rowView.findViewById(R.id.menu_subtitle);
            ImageView imageView = rowView.findViewById(R.id.image_icon);

            textViewTitle.setText(items[position]);
            textViewSubTitle.setText(subItems[position]);
            imageView.setImageResource(icons[position]);

            return rowView;
        }
    }
}
