package com.example.obrazki;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        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;
        });

        Button button = findViewById(R.id.button);
        ImageView imageView = findViewById(R.id.imageView);
        Bundle extras = getIntent().getExtras();

        int obrazekId = -1;
        if (extras != null) obrazekId = extras.getInt("obrazek", -1);

        if (obrazekId != -1){
            imageView.setImageResource(obrazekId);
        } else {
            obrazekId = R.drawable.obraz1;
        }

        int finalObrazekId = obrazekId;
        int finalObrazekId1 = obrazekId;
        imageView.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
            intent.putExtra("obrazek", finalObrazekId1);
            startActivity(intent);
        });

        button.setOnClickListener(view -> {
            int nowyObrazek = 0;
            if (finalObrazekId == R.drawable.obraz1){
                nowyObrazek = R.drawable.obraz2;
            } else if (finalObrazekId == R.drawable.obraz2){
                nowyObrazek = R.drawable.obraz1;
            }
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            intent.putExtra("obrazek", nowyObrazek);
            startActivity(intent);
        });
    }
}