package com.ycgis.macall.personalcenter.v.fragment;

import android.content.res.ColorStateList;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayoutMediator;
import com.rs.macall.androidx.basemodel.base.BaseViewBindFragment;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.databinding.FragmentMessageBinding;

import org.jetbrains.annotations.NotNull;

import java.time.format.TextStyle;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MessageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MessageFragment extends BaseViewBindFragment<FragmentMessageBinding> {

    private FragmentAdapter adapter;
    private TabLayoutMediator tabLayoutMediator;
    private String[] tableName = {"未读消息", "已读消息"};

    // TODO: Rename parameter arguments, choose names that match


    public MessageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MessageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MessageFragment newInstance() {
        return  new MessageFragment();
    }

    @Override
    protected FragmentMessageBinding getViewBinding() {
        return FragmentMessageBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    protected void readInstanceState(Bundle savedInstanceState) {

    }

    @Override
    protected void initView(View view) {

    }

    private int activeColor;
    private int normalColor;

    @Override
    protected void startView(View view) {
        activeColor = getColor(R.color.blue);
        normalColor = getColor(R.color.colorGrey6);
        viewBinding.contentTitle.setText("待办消息");
        viewBinding.msgViewPage.setOffscreenPageLimit(2);
        viewBinding.msgViewPage.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        viewBinding.msgViewPage.setUserInputEnabled(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter == null) {
            adapter = new FragmentAdapter(this);
            viewBinding.msgViewPage.setAdapter(adapter);
            tabLayoutMediator = new TabLayoutMediator(viewBinding.messageTable, viewBinding.msgViewPage, (tab, position) -> {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.widget_choose_icon_tag_bg, null);
                TextView tv = view.findViewById(R.id.choose_icon_tab_tv);
                tv.setText(tableName[position]);
                int[][] states = new int[2][];
                states[0] = new int[]{android.R.attr.state_selected};
                states[1] = new int[]{};
                int[] colors = new int[]{activeColor, normalColor};
                ColorStateList colorStateList = new ColorStateList(states, colors);
                tv.setTextColor(colorStateList);
                tab.setCustomView(view);
            });
        }

        if (tabLayoutMediator != null) {
            if (!tabLayoutMediator.isAttached()) {
                tabLayoutMediator.attach();
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (tabLayoutMediator != null) {
            tabLayoutMediator.detach();
        }
    }


    public class FragmentAdapter extends FragmentStateAdapter {

        public FragmentAdapter(@NonNull @NotNull Fragment fragment) {
            super(fragment);
        }

        @NonNull
        @NotNull
        @Override
        public Fragment createFragment(int position) {
            if (position == 0) {
                return MessageContentFragment.newInstance(false);
            }
            return MessageContentFragment.newInstance(true);
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}