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

import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ttstd.dialer.R;
import com.ttstd.dialer.activity.contact.add.ContactAddActivity;
import com.ttstd.dialer.adapter.ContactInfoAdapter;
import com.ttstd.dialer.base.mvvm.BaseMvvmActivity;
import com.ttstd.dialer.databinding.ActivityContactListBinding;
import com.ttstd.dialer.db.contact.ContactInfo;
import com.ttstd.dialer.fragment.dialog.call.CallFragment;
import com.ttstd.dialer.view.ItemTouchHelperCallback;

import java.util.List;

public class ContactListActivity extends BaseMvvmActivity<ContactListViewModel, ActivityContactListBinding> {

    private static final String TAG = "ContactListActivity";
    private static final int REQUEST_CODE_CALL = 7897;

    private ContactInfoAdapter mContactInfoAdapter;

    private List<ContactInfo> mContactInfos;

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

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

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

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

    @Override
    protected void initView() {
        mContactInfoAdapter = new ContactInfoAdapter();
        mContactInfoAdapter.setItemMoveCallback(new ContactInfoAdapter.ItemMoveCallback() {
            @Override
            public void onItemMove(int fromPosition, int toPosition) {
                Log.e(TAG, "onItemMove: ");
                ContactInfo fromContactInfo = mContactInfos.get(fromPosition);
                int fromContactPosition = fromContactInfo.getPosition();
                ContactInfo toContactInfo = mContactInfos.get(toPosition);
                int toContactPosition = toContactInfo.getPosition();
                fromContactInfo.setPosition(toContactPosition);
                mViewModel.updateItemPosition(fromContactInfo);
                toContactInfo.setPosition(fromContactPosition);
                mViewModel.updateItemPosition(toContactInfo);
            }

            @Override
            public void onItemRemoved(int position) {
                Log.e(TAG, "onItemRemoved: ");

            }
        });
        mContactInfoAdapter.setOnClickListener(new ContactInfoAdapter.OnClickListener() {
            @Override
            public void onClick(ContactInfo contactInfo) {
                new CallFragment(contactInfo).show(getSupportFragmentManager(), "CallFragment");
            }
        });
        // 设置ItemTouchHelper，实现拖动和滑动删除
        ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mContactInfoAdapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(mViewDataBinding.recyclerView);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mViewDataBinding.recyclerView.setLayoutManager(linearLayoutManager);
        mViewDataBinding.recyclerView.setAdapter(mContactInfoAdapter);
    }

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

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE_CALL) {
            // 检查所有权限的结果是否都已授予
            boolean allGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }

//            if (allGranted) {
//                onAllPermissionsGranted(); // 权限全部获取成功
//            } else {
//                // 处理权限被拒绝的情况
//                handlePermissionDenied();
//            }
        }
    }


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

    public class BtnClick {
        public void addContact(View view) {
            startActivity(new Intent(ContactListActivity.this, ContactAddActivity.class));
        }
    }
}
