package com.vpn.fastestvpnservice.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.vpn.fastestvpnservice.R;


/**
 * Created by developer007.ingic on 12/22/2017.
 */

public class CustomTextInputLayout extends LinearLayout {
    TextView tvError;
    Context ctx;

    public CustomTextInputLayout(Context context) {
        this(context, (AttributeSet) null);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs);
        this.setOrientation(VERTICAL);
        ctx = context;
    }

    public void setErrorEnabled() {
        if(this.tvError==null) {
//            this.tvError = new AppCompatTextView(ctx);
            this.removeView(this.tvError);
            this.tvError.setTextColor(ContextCompat.getColor(ctx, R.color.colorPrimary));
            this.tvError.setTextSize(10.0f);
            this.tvError.setVisibility(INVISIBLE);
            this.tvError.setPadding(14, -1, 0, 0);
            this.addView(this.tvError);
        }
    }

    public void setError(@Nullable final CharSequence error) {
        this.tvError.setText(error);
        errorEnable(true);
    }

    public void setErrorMessage(@Nullable final CharSequence error) {
        this.tvError.setText(error);
    }

    public void errorEnable(boolean isErrorShow) {
        if (isErrorShow) {
            tvError.setVisibility(VISIBLE);
        } else {
            if (tvError != null) {
                tvError.setVisibility(INVISIBLE);
                tvError.setText("");
            }
        }
    }

    public boolean isErrorEnable() {
        if (tvError.getVisibility() == VISIBLE) {
            return true;
        } else {
            return false;
        }
    }

}
