package com.zztrapp.zztrapp.ui.mine.activity;

import android.content.Intent;
import android.text.Editable;
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.R;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.MassageBean;
import com.zztrapp.zztrapp.constant.Constant;
import com.zztrapp.zztrapp.databinding.ActivityMassageBinding;
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.mine.adapter.MassageAdapter;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

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

public class MassageActivity extends BaseActivity {

    private ActivityMassageBinding binding;
    private int pageNo = 1;//页数
    private int total = 0;//总共多少页
    private int readFlag = 0;//阅读状态（0未读，1已读）
    private String searchName = "";//搜索值
    private boolean isShowDialog = true;
    private boolean isRefresh = true;//true 刷新 false 加载
    private List<MassageBean.RecordsBean> list = new ArrayList<>();
    private MassageAdapter adapter;

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

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

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

    @Override
    protected void init() {
        binding.navView.navTitle.setText("消息");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.refresh.setRefreshHeader(new ClassicsHeader(MassageActivity.this));
        binding.refresh.setRefreshFooter(new ClassicsFooter(MassageActivity.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(MassageActivity.this, LinearLayoutManager.VERTICAL, false));
        adapter = new MassageAdapter(MassageActivity.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 MassageAdapter.OnItemClickListener() {
            @Override
            public void onLookDetailClick(String id, int position) {
                Intent intent = new Intent(new Intent(MassageActivity.this, MassageDetailActivity.class));
                intent.putExtra("detail", list.get(position));
                intent.putExtra("readFlag", readFlag);
                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.nav0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.nav0Name.getPaint().setFakeBoldText(true);
                binding.nav1Name.getPaint().setFakeBoldText(false);
                binding.nav0Name.setTextColor(getResources().getColor(R.color.color2C86FF));
                binding.nav1Name.setTextColor(getResources().getColor(R.color.color232323));
                binding.line0.setBackgroundColor(getResources().getColor(R.color.color2C86FF));
                binding.line1.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
                isShowDialog = true;
                isRefresh = true;
                readFlag = 0;
                pageNo = 1;
                loadList();
            }
        });
        binding.nav1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                binding.nav0Name.getPaint().setFakeBoldText(false);
                binding.nav1Name.getPaint().setFakeBoldText(true);
                binding.nav0Name.setTextColor(getResources().getColor(R.color.color232323));
                binding.nav1Name.setTextColor(getResources().getColor(R.color.color2C86FF));
                binding.line0.setBackgroundColor(getResources().getColor(R.color.colorTransparent));
                binding.line1.setBackgroundColor(getResources().getColor(R.color.color2C86FF));
                isShowDialog = true;
                isRefresh = true;
                pageNo = 1;
                readFlag = 1;
                loadList();
            }
        });
    }

    //列表
    private void loadList() {
        ISubscriberOnNextListener<HttpResult<MassageBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<MassageBean>>() {
            @Override
            public void onNext(HttpResult<MassageBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    total = httpResult.getResult().getPages();
                    if (isRefresh) {
                        list.clear();
                    }
                    list.addAll(httpResult.getResult().getRecords());
                    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<MassageBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, isShowDialog);
        RxApiManager.get().add("findUserMessage", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).findUserMessage(httpResultProgressSubscriber, pageNo, Constant.PAGE_SIZE, searchName, readFlag);
    }

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