package com.zztrapp.zztrapp.ui.currency.fragment;

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

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
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.R;
import com.zztrapp.zztrapp.bean.BlackWaterTasksBean;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.constant.Constant;
import com.zztrapp.zztrapp.databinding.FragmentBlackWaterBinding;
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.currency.activity.EditSpecialTasksActivity;
import com.zztrapp.zztrapp.ui.currency.adapter.BlackWaterTasksAdapter;
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/6/23
 * Email bby0856@163.com
 */
public class BlackWaterFragment extends Fragment {

    private FragmentBlackWaterBinding binding;
    private BlackWaterTasksAdapter adapter;
    private int isKfbj = 1;//1是待办 可编辑的   0是已办 不可编辑
    private String searchName = "";//搜索值
    private int pageNo = 1;//页数
    private int total = 0;//总共多少页
    private boolean isShowDialog = true;
    private boolean isRefresh = true;//true 刷新 false 加载
    private List<BlackWaterTasksBean.RecordsBean> list = new ArrayList<>();


    public BlackWaterFragment() {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentBlackWaterBinding.inflate(inflater, container, false);
        init();
        onClick();
        return binding.getRoot();
    }

    private void init() {
        binding.refresh.setRefreshHeader(new ClassicsHeader(getActivity()));
        binding.refresh.setRefreshFooter(new ClassicsFooter(getActivity()));
        binding.refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isShowDialog = false;
                isRefresh = false;
                pageNo += 1;
                if (pageNo <= total) {
                    loadList();
                } else {
//                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(getResources().getString(R.string.no_more));
                }
                refreshLayout.finishLoadMore();
            }

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

        binding.rv.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new BlackWaterTasksAdapter(getContext());
        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) {
            }
        });
    }

    private void onClick() {
        Intent intent = new Intent(getContext(), EditSpecialTasksActivity.class);
        adapter.setOnItemClickListener(new BlackWaterTasksAdapter.OnItemClickListener() {
            @Override
            public void onEditClick(String id) {
                intent.putExtra("type",1);//1编辑 2查看详情
                intent.putExtra("id",id);
                startActivity(intent);
            }

            @Override
            public void onLookDetailClick(String id) {
                intent.putExtra("type",2);//1编辑 2查看详情
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        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;
            }
        });

        binding.nav1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isKfbj = 1;
                binding.nav1Name.getPaint().setFakeBoldText(true);
                binding.nav2Name.getPaint().setFakeBoldText(false);
                binding.nav1Name.setTextColor(getResources().getColor(R.color.color2C86FF));
                binding.nav2Name.setTextColor(getResources().getColor(R.color.color232323));
                binding.line1.setBackgroundColor(getResources().getColor(R.color.color2C86FF));
                binding.line2.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
                isShowDialog = true;
                isRefresh = true;
                pageNo = 1;
                loadList();
            }
        });
        binding.nav2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isKfbj = 0;
                binding.nav1Name.getPaint().setFakeBoldText(false);
                binding.nav2Name.getPaint().setFakeBoldText(true);
                binding.nav1Name.setTextColor(getResources().getColor(R.color.color232323));
                binding.nav2Name.setTextColor(getResources().getColor(R.color.color2C86FF));
                binding.line1.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
                binding.line2.setBackgroundColor(getResources().getColor(R.color.color2C86FF));
                isShowDialog = true;
                isRefresh = true;
                pageNo = 1;
                loadList();
            }
        });
    }

    //任务
    private void loadList() {
        ISubscriberOnNextListener<HttpResult<BlackWaterTasksBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<BlackWaterTasksBean>>() {
            @Override
            public void onNext(HttpResult<BlackWaterTasksBean> 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).getHcdqdjd())) {
                            list.get(i).setHcdzdwd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getHcdzdwd())));
                        }
                        if (!TextUtils.isEmpty(list.get(i).getHcdqdwd())) {
                            list.get(i).setHcdqdwd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getHcdqdwd())));
                        }
                        if (!TextUtils.isEmpty(list.get(i).getHcdzdjd())) {
                            list.get(i).setHcdzdjd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getHcdzdjd())));
                        }
                        if (!TextUtils.isEmpty(list.get(i).getHcdzdwd())) {
                            list.get(i).setHcdzdwd(MyUtil.getNumberPoint(Double.parseDouble(list.get(i).getHcdzdwd())));
                        }
                    }
                    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<BlackWaterTasksBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, getActivity(), isShowDialog);
        RxApiManager.get().add("queryPageHcstList", httpResultProgressSubscriber);
        HttpMethods.getInstance(getActivity()).queryPageHcstList(httpResultProgressSubscriber, pageNo, Constant.PAGE_SIZE, searchName, isKfbj);
    }

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

}