package com.example.lab2;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;

public class AddPhoneActivity extends AppCompatActivity {

    private EditText manufacturerInput;
    private EditText modelInput;
    private EditText androidVersionInput;
    private EditText websiteInput;
    private Button saveButton;
    private Button cancelButton;
    private Button websiteButton;

    private Phone phone;
    private boolean isEditing = false;

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

        manufacturerInput = findViewById(R.id.editManufacturer);
        modelInput = findViewById(R.id.editModel);
        androidVersionInput = findViewById(R.id.editAndroidVersion);
        websiteInput = findViewById(R.id.editWebsite);
        saveButton = findViewById(R.id.buttonSave);
        cancelButton = findViewById(R.id.buttonCancel);
        websiteButton = findViewById(R.id.buttonWebsite);

        manufacturerInput.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus && manufacturerInput.getText().toString().trim().isEmpty()) {
                manufacturerInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
            }
        });

        modelInput.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus && modelInput.getText().toString().trim().isEmpty()) {
                modelInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
            }
        });

        androidVersionInput.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus && androidVersionInput.getText().toString().trim().isEmpty()) {
                androidVersionInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
            }
        });

        websiteInput.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus && websiteInput.getText().toString().trim().isEmpty()) {
                websiteInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
            }
        });

        websiteButton.setOnClickListener(view -> {
            String url = websiteInput.getText().toString().trim();
            if (!url.startsWith("http")) {
                url = "http://" + url;
            }
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(browserIntent);
        });

        cancelButton.setOnClickListener(view -> finish());

        saveButton.setOnClickListener(view -> {
            boolean isValid = true;

            if (manufacturerInput.getText().toString().trim().isEmpty()) {
                manufacturerInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
                isValid = false;
            }

            if (modelInput.getText().toString().trim().isEmpty()) {
                modelInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
                isValid = false;
            }

            if (androidVersionInput.getText().toString().trim().isEmpty()) {
                androidVersionInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
                isValid = false;
            }

            if (websiteInput.getText().toString().trim().isEmpty()) {
                websiteInput.setError(getString(R.string.to_pole_nie_mo_e_by_puste));
                isValid = false;
            }

            if (!isValid) return;

            Intent resultIntent = new Intent();
            phone.producer = manufacturerInput.getText().toString().trim();
            phone.model = modelInput.getText().toString().trim();
            phone.version = androidVersionInput.getText().toString().trim();
            phone.website = websiteInput.getText().toString().trim();

            resultIntent.putExtra("phone", phone);
            resultIntent.putExtra("editing", isEditing);
            setResult(RESULT_OK, resultIntent);
            finish();
        });

        Intent intent = getIntent();
        if (intent.hasExtra("edited_phone")) {
            Serializable serialized = intent.getSerializableExtra("edited_phone");
            phone = (Phone) serialized;
            modelInput.setText(phone.model);
            manufacturerInput.setText(phone.producer);
            websiteInput.setText(phone.website);
            androidVersionInput.setText(phone.version);
            isEditing = true;
        } else {
            phone = new Phone("", "", "", "");
        }
    }
}
