package com.cooleshow.teacher.ui.mine;

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

import androidx.annotation.NonNull;
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.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.AddressListAdapter;
import com.cooleshow.teacher.bean.AddressBean;
import com.cooleshow.teacher.contract.AddressListContract;
import com.cooleshow.teacher.databinding.ActivitySelectAddressListBinding;
import com.cooleshow.teacher.presenter.mine.AddressListPresenter;
import com.google.gson.Gson;

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

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/6/9 10:39
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.MineCenter.MINE_SELECT_ADDRESS_LIST)
public class SelectAddressListActivity extends BaseMVPActivity<ActivitySelectAddressListBinding, AddressListPresenter> implements AddressListContract.AddressListView, View.OnClickListener {

    private AddressListAdapter addressListAdapter;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                ARouter.getInstance().build(RouterPath.MineCenter.MINE_EDIT_ADDRESS)
                        .withBoolean("isAdd",true)
                        .navigation();
                break;
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
    }

    private List<AddressBean> dataList = new ArrayList<>();

    @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(dataList);
        rvAddress.setAdapter(addressListAdapter);
        addressListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                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();

            }
        });
    }

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

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

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

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