package com.cooleshow.base.ui.activity.set;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.R;
import com.cooleshow.base.adapter.AddressListAdapter;
import com.cooleshow.base.bean.AddressBean;
import com.cooleshow.base.common.BaseConstant;
import com.cooleshow.base.constanst.AddressListContract;
import com.cooleshow.base.databinding.ActivityAddressListBinding;
import com.cooleshow.base.presenter.AddressListPresenter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.base.widgets.EmptyViewLayout;
import com.cooleshow.base.widgets.dialog.CommonDialog;
import com.google.gson.Gson;

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

/**
 * 创建日期：2022/5/17 15:16
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.BaseCenter.MINE_ADDRESS_LIST)
public class AddressListActivity extends BaseMVPActivity<ActivityAddressListBinding, AddressListPresenter> implements AddressListContract.AddressListView, View.OnClickListener {

    private AddressListAdapter addressListAdapter;
    private boolean mIsCheckAddress;
    private int delPosition;

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_add) {
            ARouter.getInstance().build(RouterPath.BaseCenter.MINE_EDIT_ADDRESS)
                    .withBoolean("isAdd", true)
                    .navigation();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
        mIsCheckAddress = getIntent().getBooleanExtra(BaseConstant.CHECK_ADDRESS, false);
    }


    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "地址列表");
        viewBinding.tvAdd.setOnClickListener(this);
        RecyclerView rvAddress = viewBinding.rvAddress;
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rvAddress.setLayoutManager(manager);
        addressListAdapter = new AddressListAdapter();
        setEmptyView();
        rvAddress.setAdapter(addressListAdapter);
        addressListAdapter.setDelClickListener(position -> {
            delPosition = position;
            AddressBean addressBean = addressListAdapter.getData().get(position);
            showTipDialog(addressBean);

        });
        addressListAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mIsCheckAddress) {
                return;
            }
            AddressBean item = (AddressBean) adapter.getItem(position);
            Gson gson = new Gson();
            String s = gson.toJson(item);
            Intent intent = new Intent();
            intent.putExtra("selectAddress", s);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    private void showTipDialog(AddressBean addressBean) {
        CommonDialog commonDialog = new CommonDialog(AddressListActivity.this);
        commonDialog.show();
        commonDialog.setTitle("提示");
        commonDialog.setContent("是否删除该地址?");
        commonDialog.setOnCancelClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commonDialog.dismiss();
            }
        });
        commonDialog.setOnConfirmClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (presenter != null) {
                    presenter.delAddress(addressBean.id);
                }
                commonDialog.dismiss();
            }
        });
    }

    private void setEmptyView() {
        EmptyViewLayout emptyView = new EmptyViewLayout(this);
        emptyView.setContent(com.cooleshow.base.R.drawable.icon_empty_content, "暂无数据");
        addressListAdapter.setEmptyView(emptyView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.companyAddressList();
    }

    @Override
    protected ActivityAddressListBinding getLayoutView() {
        return ActivityAddressListBinding.inflate(getLayoutInflater());
    }

    @Override
    protected AddressListPresenter createPresenter() {
        return new AddressListPresenter();
    }

    @Override
    public void companyAddressListSuccess(List<AddressBean> data) {
        addressListAdapter.getData().clear();
        addressListAdapter.notifyDataSetChanged();
        addressListAdapter.setNewInstance(data);
    }

    @Override
    public void delAddress() {
        if (addressListAdapter != null) {
            addressListAdapter.getData().remove(delPosition);
            addressListAdapter.notifyItemRangeRemoved(delPosition, 1);
        }
    }
}
