package com.zztrapp.zztrapp.ui.index.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

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

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.WsclssIndexBean;
import com.zztrapp.zztrapp.constant.Constant;
import com.zztrapp.zztrapp.databinding.ActivityWsclssBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.ui.index.adapter.WsclssPagelistAdapter;
import com.zztrapp.zztrapp.utilcode.mUtil.MyUtil;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

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

/**
 * Describe 污水处理设施
 * Created by Bby on 2022/7/7
 * Email bby0856@163.com
 */
public class WsclssListActivity extends BaseActivity {

    private ActivityWsclssBinding binding;
    private int pageNo = 1;//页数
    private int total = 0;//总共多少页
    private String countyCode;
    private String searchName = "";//搜索值
    private boolean isShowDialog = true;
    private boolean isRefresh = true;//true 刷新 false 加载
    private List<WsclssIndexBean.RecordsBean> list = new ArrayList<>();
    private WsclssPagelistAdapter adapter;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }


    @Override
    protected void initBinding() {
        binding = ActivityWsclssBinding.inflate(getLayoutInflater());

    }

    @Override
    protected void onResume() {
        super.onResume();
        isShowDialog = false;
        isRefresh = true;
        pageNo = 1;
        loadList();
    }

    @Override
    protected void init() {
        countyCode = getIntent().getStringExtra("countyCode");
        binding.navView.navTitle.setText("污水处理设施");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.refresh.setRefreshHeader(new ClassicsHeader(this));
        binding.refresh.setRefreshFooter(new ClassicsFooter(this));
        binding.refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = false;
                pageNo += 1;
                if (pageNo <= total) {
                    loadList();
                }
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = true;
                pageNo = 1;
                loadList();
                refreshLayout.finishRefresh();
            }
        });

        binding.rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new WsclssPagelistAdapter(this);
        binding.rv.setAdapter(adapter);
        binding.searchEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                searchName = charSequence.toString();
                if (charSequence.length() > 0) {
                    binding.del.setVisibility(View.VISIBLE);
                } else {
                    binding.del.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    @Override
    protected void initOnClick() {
        adapter.setOnItemClickListener(new WsclssPagelistAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(String id, int position) {
                Intent intent = new Intent(WsclssListActivity.this, WsclssListEditActivity.class);
                intent.putExtra("jumpType", 1);//1编辑 2查看详情
                intent.putExtra("id", id);
                intent.putExtra("navName", "编辑");
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }

            @Override
            public void onLookDetailClick(String id, int position) {
                Intent intent = new Intent(WsclssListActivity.this, WsclssListEditActivity.class);
                intent.putExtra("jumpType", 2);//1编辑 2查看详情
                intent.putExtra("id", id);
                intent.putExtra("navName", "详情");
                intent.putExtra("detail", list.get(position));
                startActivity(intent);
            }

            @Override
            public void onDailyTasksClick(String id) {
                Intent intent1 = new Intent(WsclssListActivity.this, DailyTasksActivity.class);
                intent1.putExtra("pid", id);
                startActivity(intent1);
            }
        });
        binding.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isShowDialog = true;
                isRefresh = true;
                pageNo = 1;
                loadList();
            }
        });
        binding.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchName = "";
                binding.searchEd.setText(searchName);
                binding.del.setVisibility(View.GONE);
            }
        });
        binding.searchEd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    searchName = arg0.getText().toString().trim();
                    isShowDialog = true;
                    isRefresh = true;
                    pageNo = 1;
                    loadList();
                }
                return false;
            }
        });

    }

    //列表
    private void loadList() {
        ISubscriberOnNextListener<HttpResult<WsclssIndexBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<WsclssIndexBean>>() {
            @Override
            public void onNext(HttpResult<WsclssIndexBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    total = httpResult.getResult().getPages();
                    if (isRefresh) {
                        list.clear();
                    }
                    list.addAll(httpResult.getResult().getRecords());
                    for (int i = 0; i < list.size(); i++) {
                        if (!TextUtils.isEmpty(list.get(i).getSbscwzjd())) {
                            list.get(i).setSbscwzjd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getSbscwzjd())));
                        }
                        if (!TextUtils.isEmpty(list.get(i).getSbscwzwd())) {
                            list.get(i).setSbscwzwd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getSbscwzwd())));
                        }
                    }
                    adapter.setData(list);
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<WsclssIndexBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, isShowDialog);
        RxApiManager.get().add("wsclssPagelist", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).wsclssPagelist(httpResultProgressSubscriber, pageNo, Constant.PAGE_SIZE, countyCode, searchName);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("wsclssPagelist");
    }

}