package com.ttstd.dialer.activity.contact.add;

import android.text.InputFilter;
import android.view.View;

import androidx.lifecycle.Observer;

import com.ttstd.dialer.R;
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
import com.ttstd.dialer.databinding.ActivityContactAddBinding;
import com.ttstd.dialer.filter.NoSpaceInputFilter;

public class ContactAddActivity extends BaseMvvmActivity<ContactAddViewModel, ActivityContactAddBinding> {

    @Override
    public boolean setNightMode() {
        return true;
    }

    @Override
    public boolean setfitWindow() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_contact_add;
    }

    @Override
    protected void initDataBinding() {
        mViewModel.setContext(this);
        mViewModel.setVDBinding(mViewDataBinding);
        mViewModel.setLifecycle(getLifecycleSubject());
        mViewDataBinding.setClick(new BtnClick());
    }

    @Override
    protected void initView() {
        mViewDataBinding.etName.setFilters(new InputFilter[]{new NoSpaceInputFilter()});
        mViewDataBinding.etPhone.setFilters(new InputFilter[]{new NoSpaceInputFilter()});
    }

    @Override
    protected void initData() {
        mViewModel.mIntegerMutableLiveData.observe(this, new Observer<Long>() {
            @Override
            public void onChanged(Long aLong) {
                if (aLong > 0) {
                    finish();
                }
            }
        });
    }

    public class BtnClick {
        public void save(View view) {
            String name = mViewDataBinding.etName.getText().toString();
            String phone = mViewDataBinding.etPhone.getText().toString();
            mViewModel.saveContact(name, phone);
        }
    }
}
