package com.example.punyaria;

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.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public class BerandaFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public BerandaFragment() {
        // Required empty public constructor
    }

    public static BerandaFragment newInstance(String param1, String param2) {
        BerandaFragment fragment = new BerandaFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    // Metode ini akan menangani klik pada setiap topik
    public void goToKonselingActivity(View view) {
        Class<?> targetActivity = null;

        switch (view.getId()) {
            case R.id.topic1:
                targetActivity = KekerasanFisik.class;
                break;
            case R.id.topic2:
                targetActivity = KekerasanPsikis.class;
                break;
            case R.id.topic3:
                targetActivity = KekerasanSeksual.class;
                break;
            case R.id.topic4:
                targetActivity = Penelantaran.class;
                break;
            case R.id.topic5:
                targetActivity = PerdaganganManusia.class;
                break;
            case R.id.topic6:
                targetActivity = AnakBerhadapanHukum.class;
                break;
        }

        if (targetActivity != null) {
            Intent intent = new Intent(getActivity(), targetActivity);
            startActivity(intent);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_beranda, container, false);

        // Menambahkan listener untuk setiap topik
        TextView topic1 = view.findViewById(R.id.topic1);
        TextView topic2 = view.findViewById(R.id.topic2);
        TextView topic3 = view.findViewById(R.id.topic3);
        TextView topic4 = view.findViewById(R.id.topic4);
        TextView topic5 = view.findViewById(R.id.topic5);
        TextView topic6 = view.findViewById(R.id.topic6);

        topic1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToKonselingActivity(v);
            }
        });

        topic2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToKonselingActivity(v);
            }
        });

        topic3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToKonselingActivity(v);
            }
        });

        topic4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToKonselingActivity(v);
            }
        });

        topic5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToKonselingActivity(v);
            }
        });

        topic6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToKonselingActivity(v);
            }
        });

        TextView hotlineTextView = view.findViewById(R.id.text_menu_kontak);
        TextView chatTextView = view.findViewById(R.id.text_menu_chat);
        TextView laporkanTextView = view.findViewById(R.id.text_menu_laporkan);

        hotlineTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HotlineActivity.class);
                startActivity(intent);
            }
        });

        chatTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), KonselingActivity.class);
                startActivity(intent);
            }
        });

        laporkanTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Nama paket aplikasi yang akan dibuka
                String appPackageName = "com.lindungianakdanperempuan.www";

                // Membuat intent untuk membuka aplikasi jika sudah terpasang
                Intent appIntent = getActivity().getPackageManager().getLaunchIntentForPackage(appPackageName);
                if (appIntent != null) {
                    // Jika aplikasi sudah terinstal, langsung buka aplikasi
                    startActivity(appIntent);
                } else {
                    // Jika aplikasi belum terinstal, buka halaman aplikasi di Google Play Store
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
                    } catch (android.content.ActivityNotFoundException e) {
                        // Jika tidak ada aplikasi Play Store yang terpasang, membuka halaman aplikasi di web browser
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
                    }
                }
            }
        });

        return view;
    }
}
