package com.cooleshow.teacher.ui.minestyle;

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.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.adapter.TeachableInstrumentAdapter;
import com.cooleshow.teacher.bean.TeachableInstrumentBean;
import com.cooleshow.teacher.contract.TeachableInstrumentContract;
import com.cooleshow.teacher.databinding.ActivityTeachableInstrumentBinding;
import com.cooleshow.teacher.presenter.minestyle.TeachableInstrumentPresenter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/5/13 15:25
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.MineCenter.MINE_TEACHABLE_INSTRUMENT)
public class TeachableInstrumentActivity extends BaseMVPActivity<ActivityTeachableInstrumentBinding, TeachableInstrumentPresenter> implements TeachableInstrumentContract.TeachableInstrumentView, View.OnClickListener {
    private RecyclerView instrumentList;
    private TeachableInstrumentAdapter teachableInstrumentAdapter;
    @Autowired(name = "bundle")
    Bundle myBundle;

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                List<TeachableInstrumentBean> selectTeachableInstrument = new ArrayList<>();

                for (TeachableInstrumentBean teachableInstrumentBean : dataList) {
                    if (null != teachableInstrumentBean.subjects && teachableInstrumentBean.subjects.size() > 0) {
                        for (TeachableInstrumentBean subject : teachableInstrumentBean.subjects) {
                            if (subject.isSelect) {
                                selectTeachableInstrument.add(subject);
                            }
                        }
                    }
                }
                Intent intent = new Intent();
                intent.putExtra("selectTeachableInstrument", (Serializable) selectTeachableInstrument);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }


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

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        initCarryData();
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "可教授乐器");
        instrumentList = viewBinding.rvInstrument;
        viewBinding.tvConfirm.setOnClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        instrumentList.setLayoutManager(manager);
        teachableInstrumentAdapter = new TeachableInstrumentAdapter(dataList,carryList.size());
        instrumentList.setAdapter(teachableInstrumentAdapter);
    }

    private List<TeachableInstrumentBean> carryList = new ArrayList<>();

    private void initCarryData() {
        if (null != myBundle) {
            carryList = (List<TeachableInstrumentBean>) myBundle.getSerializable("selectTeachableInstrument");
            if (null == carryList) {
                carryList = new ArrayList<>();
            }
        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarDarkTheme(this, true);
    }

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

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

    @Override
    public void initData() {
        super.initData();
        presenter.subjectSelect();
    }

    @Override
    public void subjectSelectSuccess(List<TeachableInstrumentBean> data) {
        if (null != data && data.size() > 0) {
            dataList.clear();
            for (TeachableInstrumentBean datum : data) {
                if (null != datum.subjects && datum.subjects.size() > 0) {
                    for (TeachableInstrumentBean subject : datum.subjects) {
                        for (TeachableInstrumentBean teachableInstrumentBean : carryList) {
                            if (teachableInstrumentBean.id == subject.id) {
                                subject.isSelect = true;
                            }
                        }
                    }
                    dataList.add(datum);
                }
            }

            if (null != teachableInstrumentAdapter) {
                teachableInstrumentAdapter.notifyDataSetChanged();
            }
        }
    }
}
