package com.halo.assistant.fragment.user.region;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

import com.gh.gamecenter.common.base.fragment.BaseFragment;
import com.gh.gamecenter.common.view.VerticalItemDecoration;
import com.gh.gamecenter.R;

import java.util.List;

/**
 * Created by khy on 25/09/17.
 */
public class RegionCountFragment extends BaseFragment implements OnRegionSelectListener {

    private TextView mRvTitle;

    private final int mRegionCount = 34; // 省份数量

    private LinearLayoutManager mLayoutManager;
    private RelativeLayout.LayoutParams mParams;

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerView recyclerView = view.findViewById(R.id.region_rv);
        mRvTitle = view.findViewById(R.id.region_rv_title);

        mParams = (RelativeLayout.LayoutParams) mRvTitle.getLayoutParams();
        mRvTitle.setVisibility(View.VISIBLE);
        mLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(new RegionCounAdapter(getContext(), this));
        recyclerView.addItemDecoration(new VerticalItemDecoration(getContext(), false));
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int position = mLayoutManager.findFirstVisibleItemPosition();
                if (position == mRegionCount) {
                    mRvTitle.setText("国内");
                    int bottom = mLayoutManager.findViewByPosition(position).getBottom();
                    if (bottom <= mRvTitle.getHeight()) {
                        mParams.topMargin = bottom - mRvTitle.getHeight();
                        mRvTitle.setLayoutParams(mParams);
                    } else {
                        mParams.topMargin = 0;
                        mRvTitle.setLayoutParams(mParams);
                    }
                } else {
                    mParams.topMargin = 0;
                    mRvTitle.setLayoutParams(mParams);
                    if (position > mRegionCount) {
                        mRvTitle.setText("国外");
                    } else {
                        mRvTitle.setText("国内");
                    }
                }
            }
        });
    }

    @Override
    public void onRegionSelect(String content, List<String> provinceList) {
        if (getParentFragment() instanceof OnRegionSelectListener) {
            ((OnRegionSelectListener) getParentFragment()).onRegionSelect(content, provinceList);
        }
    }

}
