package com.zhaohaoting.framework.abs;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.abs.adapter.SimpleListAdapter;
import com.zhaohaoting.framework.utils.Utils;
import com.zhaohaoting.framework.view.QuickIndexBar;

/**
 * author：Created by ZHT on 2018/1/9 15:50
 * email：526309416@qq.com
 * desc：带索引条的activity
 */
public abstract class QuickBarListActivity<DATA extends QuickIndexBar.QuickIndexEntity> extends BaseActivity implements AdapterView.OnItemClickListener {
    protected ListView listView;
    protected QuickIndexBar indexBar;
    protected TextView selectedWord;
    protected TextView index;
    protected SimpleListAdapter<DATA> adapter;
    protected FrameLayout fl_progress;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_quickbar_list;
    }

    @Override
    protected void initialize() {
        listView = findViewById(R.id.list_view);
        indexBar = findViewById(R.id.indexBar);
        selectedWord = findViewById(R.id.selectedWord);
        index = findViewById(R.id.index);
        fl_progress = findViewById(R.id.fl_progress);

        listView.setAdapter(adapter = getAdapter());
        // 设置监听
        indexBar.setListener(letter -> {
            showLetter(letter);
            // 根据字母定位ListView, 找到集合中第一个以letter为拼音首字母的对象,得到索引
            for (int i = 0; i < adapter.getData().size(); i++) {
                DATA data = adapter.getData().get(i);
                if (data == null) continue;
                String l = data.getFirstLetter();//大写转换
                if (TextUtils.equals(letter, l)) {
                    // 匹配成功
                    listView.setSelection(i);
                    break;
                }
            }
        });
        if (!Utils.listIsEmpty(adapter.getData())) {
            DATA data = adapter.getData().get(0);
            index.setText(data.getFirstLetter());
        }

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            private int firstVisibleItem = 0;

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem != this.firstVisibleItem) {
                    this.firstVisibleItem = firstVisibleItem;
                    DATA data = adapter.getData().get(firstVisibleItem);
                    index.setText(data.getFirstLetter());
                }
            }
        });
        listView.setOnItemClickListener(this);
        init();
    }

    @Override
    public final void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int headCount = listView.getHeaderViewsCount();
        if (position < headCount) return;
        position = position - headCount;
        itemClicked(parent, view, position, id);
    }

    protected void itemClicked(AdapterView<?> parent, View view, int position, long id) {
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    indexBar.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };

    /**
     * 显示字母
     *
     * @param letter
     */
    protected void showLetter(String letter) {
        selectedWord.setVisibility(View.VISIBLE);
        selectedWord.setText(letter);

        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                selectedWord.setVisibility(View.GONE);
            }
        }, 2000);
    }

    protected abstract void init();

    protected abstract SimpleListAdapter getAdapter();
}
