package com.sipai.jspatrol.Fault;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.sipai.jspatrol.Bean.User;
import com.sipai.jspatrol.Fault.adapter.PersonSelectorAdapter;
import com.sipai.jspatrol.R;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PersonSelectorActivity extends AppCompatActivity {
    private List<User> users = new ArrayList<>();
    private List<User> search_users = new ArrayList<>();
    private List<User> sel_users = new ArrayList<>();
    private RecyclerView rc_view;
    private PersonSelectorAdapter adapter;
    private LinearLayoutManager manager;
    private ImageView iv_search;
    private EditText edt_name;
    private String sel_mark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_selector);
        init();
        initView();
    }

    private void initView() {
        ImageView btn_back = (ImageView) findViewById(R.id.moudle_ps_iv_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tv_save = (TextView) findViewById(R.id.moudle_ps_tv_save);
        tv_save.setText("保存");
        tv_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sel_users.clear();
                Map<Integer, Boolean> map1 = adapter.getMap();
                for (int i = map1.size()-1; 0<=i; i--) {
                    if (map1.get(i)) {
                        Log.d("TAG", "你选了第：" + i + "项");
                        if (sel_mark.equals("all")) {
                            sel_users.add(users.get(i));
                        }else {
                            sel_users.add(search_users.get(i));
                        }
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("sel", (Serializable) sel_users);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        iv_search = findViewById(R.id.moudle_ps_iv_search);
        edt_name = findViewById(R.id.moudle_ps_edt_name);
        edt_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().isEmpty()) {
                    checkUsers(String.valueOf(s));
                }else {
                    showAll();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        rc_view = findViewById(R.id.moudle_person_selector_rc);
        adapter = new PersonSelectorAdapter(users,PersonSelectorActivity.this);
        rc_view.setAdapter(adapter);
        manager = new LinearLayoutManager(this);
        rc_view.setLayoutManager(manager);
        sel_mark = "all";
    }

    private void showAll() {
        if (!PersonSelectorActivity.this.isFinishing()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    sel_mark = "all";
                    adapter = new PersonSelectorAdapter(users,PersonSelectorActivity.this);
                    rc_view.setAdapter(adapter);
                }
            });
        }
    }

    private void checkUsers(String s) {
        search_users = new ArrayList<>();
        for (int i=0;i<users.size();i++){
            if (users.get(i).getCaption().contains(s)){
                search_users.add(users.get(i));
            }
        }
        sel_mark = "sel";
        adapter = new PersonSelectorAdapter(search_users,PersonSelectorActivity.this);
        rc_view.setAdapter(adapter);
    }

    /**
     * 初始化
     */
    private String mServer;
    private String mUsername;
    private String mPassword;
    private String mUserid;
    private String mCaption;
    private String mBizid;
    private String mGroup;
    private String role;
    public void init(){
        SharedPreferences sharedPre=getSharedPreferences("config", MODE_PRIVATE);
        role = sharedPre.getString("role","");
        mServer=sharedPre.getString("server", "");
        mUsername=sharedPre.getString("username", "");
        mPassword=sharedPre.getString("password", "");
        mUserid=sharedPre.getString("userid", "");
        mCaption=sharedPre.getString("caption", "");
        mBizid=sharedPre.getString("bizid", "");
        mGroup=sharedPre.getString("group", "");
        users = (List<User>) getIntent().getSerializableExtra("users");
    }
}
