package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity4 extends AppCompatActivity {

    private TextView txtBaslik, txtKategori, txtEkleyen, txtMalzemeler, txtYapilis, txtEmoji;
    private ImageView imgPoster;
    private TextView btnBackCircle;
    private Button btnKapat;

    private ExecutorService executorService = Executors.newSingleThreadExecutor();

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

        txtBaslik = findViewById(R.id.txtDetayBaslik);
        txtKategori = findViewById(R.id.txtDetayKategori);
        txtEkleyen = findViewById(R.id.txtDetayEkleyen);
        txtMalzemeler = findViewById(R.id.txtDetayMalzemeler);
        txtYapilis = findViewById(R.id.txtDetayYapilis);
        txtEmoji = findViewById(R.id.txtDetayEmoji);
        imgPoster = findViewById(R.id.imgDetayPoster);
        btnBackCircle = findViewById(R.id.btnDetayBackCircle);
        btnKapat = findViewById(R.id.btnDetayKapat);

        Intent intent = getIntent();
        if (intent != null) {
            String ad = intent.getStringExtra("pAd");
            String mal = intent.getStringExtra("pMal");
            String yap = intent.getStringExtra("pYap");
            String kategori = intent.getStringExtra("pKategori");
            String resimUrl = intent.getStringExtra("pResimUrl");
            String ekleyen = intent.getStringExtra("pEkleyen");

            txtBaslik.setText(ad);
            txtKategori.setText(kategori == null ? "Kategori: Diğer" : "Kategori: " + kategori);
            txtEkleyen.setText("Şef: " + (ekleyen == null ? "Bilinmeyen" : ekleyen));
            txtMalzemeler.setText(mal);
            txtYapilis.setText(yap);

            // Kategori Emojisi set et
            txtEmoji.setText(MainActivity.getEmojiForCategory(kategori));

            // Resim URL yükle
            if (resimUrl != null && !resimUrl.trim().isEmpty()) {
                loadRemoteImage(resimUrl);
            }
        }

        // Geri Dön butonu olayları
        btnBackCircle.setOnClickListener(v -> finish());
        btnKapat.setOnClickListener(v -> finish());
    }

    private void loadRemoteImage(String urlString) {
        executorService.execute(() -> {
            try {
                URL url = new URL(urlString);
                InputStream is = url.openStream();
                Bitmap bmp = BitmapFactory.decodeStream(is);
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (bmp != null) {
                        imgPoster.setImageBitmap(bmp);
                        imgPoster.setVisibility(View.VISIBLE);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}