package com.sheep.gamegroup.view.dialog;

import android.app.ActionBar;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.sheep.gamegroup.view.adapter.ArrayAdapter;
import com.sheep.jiuyan.samllsheep.R;

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

/**
 * popup 弹框列表
 * Created by ljy on 2018/7/11.
 */

public class SelfPopupWindow {
    private Activity activity;

    private static SelfPopupWindow selfPopupWindow;

    public static SelfPopupWindow newInstance(){
        if(selfPopupWindow == null)
            selfPopupWindow = new SelfPopupWindow();
        return selfPopupWindow;
    }

    public SelfPopupWindow() {
    }

    /** TextView选择框 */
    private View mSelectTv;

    /** popup窗口里的ListView */
    private ListView myListview;

    /** popup窗口 */
    private PopupWindow typeSelectPopup;

    /** 模拟的假数据 */
    private List<String> testData;

    /** 数据适配器 */
    private ArrayAdapter<Object> testDataAdapter;

    private AdapterView.OnItemClickListener  onItemClickListener;


    public SelfPopupWindow SelfPopupWindow(Activity activity, View view, AdapterView.OnItemClickListener  onItemClickListener, ArrayAdapter testDataAdapter) {
        this.mSelectTv = view;
        this.activity = activity;
        this.onItemClickListener = onItemClickListener;
        this.testDataAdapter = testDataAdapter;
        return selfPopupWindow;
    }

    public SelfPopupWindow initPopup(){
        myListview = new ListView(activity);
        TestData();
        myListview.setAdapter(testDataAdapter);
        myListview.setOnItemClickListener(onItemClickListener);

        typeSelectPopup = new PopupWindow(myListview, mSelectTv.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(activity, R.drawable.x_shap_shadow_bg_rectgangle_white);
        typeSelectPopup.setBackgroundDrawable(drawable);
        typeSelectPopup.setFocusable(true);
        typeSelectPopup.setOutsideTouchable(true);
        typeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
        return selfPopupWindow;
    }


    /**
     * 模拟假数据
     */
    private void TestData() {
        testData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            String str = new String("数据" + i);
            testData.add(str);
        }
    }

    public void showPop(){

        if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
            typeSelectPopup.showAsDropDown(mSelectTv, 0, 2);
        }
    }

    public void hidePop(){
        if (typeSelectPopup != null && typeSelectPopup.isShowing()) {
            typeSelectPopup.dismiss();
        }
    }
}
