package com.philisense.rdgo.ui.waitPutIn;

import android.app.Activity;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;

import com.philisense.rdgo.R;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.PeopleBean;
import com.philisense.rdgo.databinding.ActivityPeopleXuanzeBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.viewModel.HQTViewModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class peopleActivity extends AppCompatActivity {

    ActivityPeopleXuanzeBinding mBinding;

    public HQTViewModel viewModel;
    String id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (getIntent() != null) {
            id = getIntent().getStringExtra("id");
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_people_xuanze);
        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("人物选择");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finish();
            }
        });

        viewModel = new HQTViewModel(this);
        getData();

    }


    private void getData() {

//        final ProgressDialog dialog = ProgressDialog.show(JiDiActivity.this, "提示", "正在录入系统..");
        viewModel.getPeople(new RequestImpl<List<PeopleBean.ListBean>>() {
            @Override
            public void loadSuccess(List<PeopleBean.ListBean> bean) {
                if (bean != null) {
                    set(bean);
                }
            }

            @Override
            public void loadFailed(OError error) {

            }
        });

    }

    List<String> mData;

    private void set(List<PeopleBean.ListBean> bean) {
        mData = new ArrayList<>();

        for (int i = 0; i < bean.size(); i++) {
            mData.add("名字：" + bean.get(i).getPersonName());
        }
        //☆☆☆：通过listView设置多选模式
        if ("1".equals(id)) {
            //☆☆☆:通过listView设置单选模式
            mBinding.lvSingle.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        } else {
            //☆☆☆：通过listView设置多选模式
            mBinding.lvSingle.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        }

        ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item_people);
        adapter.addAll(mData);
        mBinding.lvSingle.setAdapter(adapter);

        mBinding.sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<PeopleBean.ListBean> n = isSingle(bean);
                run(n);
            }
        });
    }

    private void run(List<PeopleBean.ListBean> n) {

        Intent mIntent = new Intent();
        mIntent.putExtra("data",  (Serializable) n);
        // 设置结果，并进行传送
        this.setResult(0, mIntent);
        this.finish();

    }
    /**
     * 确定按钮的监听事件
     * @param v
     */
    /**
     * 确定按钮的监听事件
     *
     * @param
     */

    public ArrayList<PeopleBean.ListBean> isSingle(List<PeopleBean.ListBean> bean) {
        ArrayList<PeopleBean.ListBean> n = new ArrayList<>();
        //通过listView对象获取到当前listView中被选择的条目position;
        //以下方法实现会对返回一个SparseBooleanArray集合，其中对listview的触发过点击事件的每个条目进行
        // 标记（键值对）键==position/值==boolean，若该条目被选中则显示true，否则显示false;
        SparseBooleanArray checkedItemPositions = mBinding.lvSingle.getCheckedItemPositions();
        //循环遍历集合中所有的数据，获取每个item是否在SparseBooleanArray存储，以及对应的值；
        for (int i = 0; i < mData.size(); i++) {
            //根据key获取对应的boolean值，没有则返回false
            Boolean aBoolean = checkedItemPositions.get(i);
//            Log.e("i:"+i , aBoolean +"");
            if (aBoolean) {
                n.add(bean.get(i));
            }
        }
        return n;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * @param context activity
     */
    public static void start(Activity context, String id, int num) {
        Intent intent = new Intent(context, peopleActivity.class);
        intent.putExtra("id", id);
        ActivityCompat.startActivityForResult(context, intent, num, null);
    }


}
