package com.smaato.sdk.demoapp.interstitial;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.smaato.sdk.demoapp.AdConfig;
import com.smaato.sdk.demoapp.R;
import com.smaato.sdk.demoapp.utils.BaseTextWatcher;

import java.util.Arrays;

public class CreateInterstitialAdConfigActivity extends AppCompatActivity {

    public static final String NEW_CONFIG = "new_config";
    public static final String OLD_CONFIG = "old_config";

    private static final int MAX_INPUT_LENGTH_ID = 20;
    private static final int MAX_INPUT_LENGTH_NAME = 30;

    @NonNull
    private EditText adSpaceIdEditText;
    @NonNull
    private EditText publisherIdEditText;
    @NonNull
    private EditText nameEditText;
    @NonNull
    private final TextWatcher textWatcher = new BaseTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            supportInvalidateOptionsMenu();
        }
    };
    @Nullable
    private AdConfig oldAdConfig;

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

        adSpaceIdEditText = findViewById(R.id.adspace_id);
        publisherIdEditText = findViewById(R.id.publisher_id);
        nameEditText = findViewById(R.id.name);

        if (getIntent() != null) {
            oldAdConfig = getIntent().getParcelableExtra(OLD_CONFIG);
        }

        String adSpace = oldAdConfig != null ? oldAdConfig.adSpaceId : "";
        String publisherId = oldAdConfig != null ? oldAdConfig.publisherId : "";
        String name = oldAdConfig != null ? oldAdConfig.name : "";

        initEditText(adSpaceIdEditText, MAX_INPUT_LENGTH_ID, textWatcher, adSpace);
        initEditText(publisherIdEditText, MAX_INPUT_LENGTH_ID, textWatcher, publisherId);
        initEditText(nameEditText, MAX_INPUT_LENGTH_NAME, textWatcher, name);

        invalidateOptionsMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(@NonNull Menu menu) {
        getMenuInflater().inflate(R.menu.create_custom_ad_config_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
        boolean isDataValid = true;

        for (EditText editText : Arrays.asList(nameEditText, publisherIdEditText, adSpaceIdEditText)) {
            if (editText.getText().toString().trim().isEmpty()) {
                isDataValid = false;
                break;
            }
        }

        menu.findItem(R.id.save).setEnabled(isDataValid);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.save:
                Intent data = new Intent();
                data.putExtra(NEW_CONFIG, new AdConfig(
                        nameEditText.getText().toString().trim(),
                        publisherIdEditText.getText().toString().trim(),
                        adSpaceIdEditText.getText().toString().trim()));
                data.putExtra(OLD_CONFIG, oldAdConfig);
                setResult(RESULT_OK, data);
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void initEditText(@NonNull EditText editText, int maxLength, @NonNull TextWatcher textWatcher, @NonNull String value) {
        InputFilter[] initialEditTextKeywordsFilters = editText.getFilters();
        InputFilter[] editTextKeywordsFilters = Arrays.copyOf(initialEditTextKeywordsFilters, initialEditTextKeywordsFilters.length + 1);
        editTextKeywordsFilters[editTextKeywordsFilters.length - 1] = new InputFilter.LengthFilter(maxLength);
        editText.setFilters(editTextKeywordsFilters);
        editText.addTextChangedListener(textWatcher);
        editText.setText(value);
    }
}
