package com.ttstd.dialer.fragment.contact;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.ttstd.dialer.R;
import com.ttstd.dialer.adapter.HomeContactAdapter;
import com.ttstd.dialer.base.mvvm.fragment.BaseMvvmFragment;
import com.ttstd.dialer.databinding.FragmentContactBinding;
import com.ttstd.dialer.db.contact.ContactInfo;
import com.ttstd.dialer.fragment.dialog.call.CallFragment;
import com.ttstd.dialer.view.EqualHeightDecoration;

import java.util.List;

public class ContactFragment extends BaseMvvmFragment<ContactViewModel, FragmentContactBinding> {
    private static final String TAG = "ContactFragment";

    private Activity mContext;
    private HomeContactAdapter mHomeContactAdapter;
    private List<ContactInfo> mContactInfos;

    private static final int REQUEST_CODE_CALL = 7897;


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

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

    @Override
    protected void initView(Bundle bundle) {
        mHomeContactAdapter = new HomeContactAdapter();
        mHomeContactAdapter.setOnClickListener(new HomeContactAdapter.OnClickListener() {
            @Override
            public void onClick(ContactInfo contactInfo) {
                new CallFragment(contactInfo).show(getChildFragmentManager(), "CallFragment");
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        mViewDataBinding.recyclerView.addItemDecoration(new EqualHeightDecoration(3));
        mViewDataBinding.recyclerView.setLayoutManager(gridLayoutManager);
        mViewDataBinding.recyclerView.setAdapter(mHomeContactAdapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mViewModel.mContactListData.observe(this, new Observer<List<ContactInfo>>() {
            @Override
            public void onChanged(List<ContactInfo> contactInfos) {
                Log.e(TAG, "mContactListData: " + contactInfos);
                mContactInfos = contactInfos;
                mHomeContactAdapter.setContactInfos(mContactInfos);
            }
        });
    }


    @Override
    public void fetchData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        mViewModel.getAllContacts();
    }

    public class BtnClick {

    }
}
