package org.deke.risk.riskahead.helper;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.app.AlertDialog;
import com.google.android.material.textfield.TextInputLayout;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Patterns;
import android.widget.Button;

import org.deke.risk.riskahead.R;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class ValidatedEditTextPreference extends EditTextPreference {
    private final static int TYPE_VARIATION_EMAIL_ADDRESS = 33;

    public ValidatedEditTextPreference(Context ctx, AttributeSet attrs, int defStyle)
    {
        super(ctx, attrs, defStyle);
    }

    public ValidatedEditTextPreference(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);
    }

    private class EditTextWatcher implements TextWatcher
    {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count){}

        @Override
        public void beforeTextChanged(CharSequence s, int start, int before, int count){}

        @Override
        public void afterTextChanged(Editable s)
        {
            onEditTextChanged();
        }
    }
    EditTextWatcher m_watcher = new EditTextWatcher();

    /**
     * Return true in order to enable positive button or false to disable it.
     */
    protected boolean onCheckValue(String value)
    {
        boolean enable = true;

        if((getEditText().getInputType() & InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS) != 0){
            if (!Patterns.EMAIL_ADDRESS.matcher(value).matches()) {
                if(getEditText().getParent().getClass() == TextInputLayout.class){
                    ((TextInputLayout) getEditText().getParent()).setError(getEditText().getContext().getResources().getString(R.string.error_email));
                }else{
                    getEditText().setError(getEditText().getContext().getResources().getString(R.string.error_email));
                }
                enable = false;
            }
        }else{
            if (value.length() < 5) {
                getEditText().setError(getEditText().getContext().getResources().getString(R.string.error_anytext));
                enable = false;
            }
        }
        Log.d("SettingValidator", "inputtype: "+getEditText().getInputType());
        return enable;
    }

    protected void onEditTextChanged()
    {
        boolean enable = onCheckValue(getEditText().getText().toString());

        Dialog dlg = getDialog();

        if(dlg instanceof AlertDialog){
            AlertDialog alertDlg = (AlertDialog)dlg;
            Button btn = alertDlg.getButton(AlertDialog.BUTTON_POSITIVE);
            btn.setEnabled(enable);
            Log.d("SettingValidator", "btnSetEnable durchgeführt: "+btn.isEnabled());
        }

    }

    @Override
    protected void showDialog(Bundle state)
    {
        super.showDialog(state);

        getEditText().removeTextChangedListener(m_watcher);
        getEditText().addTextChangedListener(m_watcher);
        onEditTextChanged();
    }
}
