package com.common.im.ui.contact;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.common.im.adapter.ContactListTabPagerAdapter;
import com.common.im.presenter.ContactListTabPresenter;
import com.common.im_ui.R;
import com.common.im_ui.databinding.FragmentContactListTabBinding;
import com.cooleshow.base.ui.fragment.BaseMVPFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 创建日期：2022/6/9 18:01
 *
 * @author Ryan
 * 类说明：
 */
public class ContactListTabFragment extends BaseMVPFragment<FragmentContactListTabBinding, ContactListTabPresenter> implements View.OnClickListener {
    private boolean isSelectContact = false;
    public static final String IS_SELECT_CONTACT = "IS_SELECT_CONTACT";

    @Override
    protected FragmentContactListTabBinding getLayoutView() {
        return FragmentContactListTabBinding.inflate(getLayoutInflater());
    }

    @Override
    protected ContactListTabPresenter createPresenter() {
        return new ContactListTabPresenter();
    }

    private ViewPager viewPager;

    @Override
    protected void initView(View rootView) {
        if (getArguments() != null) {
            isSelectContact = getArguments().getBoolean(IS_SELECT_CONTACT, false);
        }
        viewPager = mViewBinding.viewpager;
        mViewBinding.llRoom.setOnClickListener(this);
        mViewBinding.llSingle.setOnClickListener(this);
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    selectSingle();
                } else {
                    selectRoom();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void selectSingle() {
        mViewBinding.imContactRoom.setBackgroundResource(R.drawable.icon_contact_room_normal);
        mViewBinding.imContactSingle.setBackgroundResource(R.drawable.icon_contact_single_select);
        mViewBinding.tvContactRoom.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_666666));
        mViewBinding.tvContactSingle.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
    }

    private void selectRoom() {
        mViewBinding.imContactRoom.setBackgroundResource(R.drawable.icon_contact_room_select);
        mViewBinding.imContactSingle.setBackgroundResource(R.drawable.icon_contact_single_normal);
        mViewBinding.tvContactRoom.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_2dc7aa));
        mViewBinding.tvContactSingle.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_666666));
    }

    @Override
    protected void initData() {
        initTabLayoutAndViewPager();
    }

    private List<Fragment> fragments = new ArrayList<>();
    private List<String> titles = new ArrayList<String>(Arrays.asList("联系人", "群聊"));

    private void initTabLayoutAndViewPager() {
        fragments.clear();
        ContactPersonListFragment contactPersonListFragment = new ContactPersonListFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(IS_SELECT_CONTACT, isSelectContact);
        contactPersonListFragment.setArguments(bundle);
        fragments.add(contactPersonListFragment);
        Bundle bundle2 = new Bundle();
        bundle2.putBoolean(IS_SELECT_CONTACT, isSelectContact);
        ContactRoomListFragment contactRoomListFragment = new ContactRoomListFragment();
        contactRoomListFragment.setArguments(bundle2);
        fragments.add(contactRoomListFragment);

        viewPager.setAdapter(new ContactListTabPagerAdapter(getParentFragmentManager(), fragments, titles));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_single) {
            viewPager.setCurrentItem(0);
        } else if (id == R.id.ll_room) {
            viewPager.setCurrentItem(1);
        }
    }

    public void refreshContactList() {
        if (fragments.size() > 0) {
            ContactPersonListFragment fragment = (ContactPersonListFragment) fragments.get(0);
            if (fragment != null) {
                fragment.initData();
            }
        }
    }
}
