package ch.pizzalemon.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import androidx.core.content.ContextCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;
import ch.pizzalemon.android.helper.PasswordHelper;

/**
 * Created by cimenmus on 28/09/2017.
 */

public class AppEditText extends LinearLayout implements View.OnClickListener {

    private View rootView;
    private TextView hintTextView;
    private EditText editText;
    private RelativeLayout bottomLineLayout;
    private boolean isPasswordHidden = true;
    private Typeface typeFace;
    private String hint;
    private String inputType;
    private String edittextTheme;
    private int characterCount;

    /*
    public PizzalinkEditText(LinearLayout rootView, boolean isLeftIconVisible, int leftIconId,
                          int editTextHintId, int inputType, boolean isPasswordIconVisible){

        initViews(rootView);
        setLeftIcon(isLeftIconVisible, leftIconId);
        setPasswordIcon(isPasswordIconVisible);
        setEditTextt(editTextHintId, inputType);
    }
    */

    public AppEditText(Context context) {
        super(context);
        init(context);
    }

    public AppEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AppEditText, 0, 0);
        try {
            hint = a.getString(R.styleable.AppEditText_hint);
            characterCount = a.getInt(R.styleable.AppEditText_characterCount, -1);
            inputType = a.getString(R.styleable.AppEditText_inputType);
            edittextTheme = a.getString(R.styleable.AppEditText_edittextTheme);
        } finally {
            a.recycle();
        }
        init(context);
    }

    private void init(Context context) {
        rootView = inflate(context, R.layout.layout_pizzalink_edittext, this);
        hintTextView = (TextView) rootView.findViewById(R.id.hintTextView);
        editText = (EditText) rootView.findViewById(R.id.editText);
        bottomLineLayout = (RelativeLayout) rootView.findViewById(R.id.bottomLineLayout);
        hintTextView.setText(hint);
        if(edittextTheme != null && edittextTheme.equals("navy")){
            int navyColor = ContextCompat.getColor(BaseActivity.currentActivity, R.color.navigation_drawer_background);
            hintTextView.setTextColor(navyColor);
            editText.setTextColor(navyColor);
            bottomLineLayout.setBackgroundColor(navyColor);
        }

        if(characterCount != -1){
            int maxLength = characterCount;
            editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
        }
        setInputType();
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                focusEditText();
            }
        });
    }

    private void setInputType(){
        if (inputType == null)
            inputType = "text";
        switch (inputType){
            case "name":
                editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
                        InputType.TYPE_TEXT_FLAG_CAP_WORDS);
                break;
            case "password":
                editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case "phone":
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_PHONE);
                break;
            case "email":
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                break;
            case "number":
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_NUMBER);
                break;
            case "postcode":
                int maxLength = 4;
                editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(maxLength)});
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_NUMBER);
                break;
            case "address":
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_FLAG_CAP_WORDS | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                goNextLineWhenEndOfLine();
                break;
            case "multiline":
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_FLAG_MULTI_LINE);
                goNextLineWhenEndOfLine();
                break;
            default:
                editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS | InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
                break;
        }
    }


    @Override
    public void onClick(View v) {
        hideShowPassword();
    }

    private void hideShowPassword(){
        if(isPasswordHidden)
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        else
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        isPasswordHidden = !isPasswordHidden;
        editText.setTypeface(typeFace);
    }

    private void focusEditText(){
        editText.requestFocus();
        InputMethodManager inputMethodManager=(InputMethodManager)BaseActivity.currentActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
    }

    public void setHint(int hintResId){
        hintTextView.setText(hintResId);
    }

    private void goNextLineWhenEndOfLine(){
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable editable) {
                // if edittext has 10chars & this is not called yet, add new line
                if(editText.getText().length() == 40 * editText.getLineCount()) {
                    editText.append("\n");
                }
            }
        });
    }

    public boolean isEmpty(){
        return editText.getText().toString().isEmpty();
    }

    public boolean isEmail(){
        return PasswordHelper.EMAIL_ADDRESS_PATTERN.matcher(editText.getText()).matches();
    }

    public String getText(){
        return editText.getText().toString();
    }

    public void setText(String text){
        editText.setText(text);
    }

    public TextView getHintTextView() {
        return hintTextView;
    }

    public EditText getEditText() {
        return editText;
    }
}