package com.cooleshow.student.ui.mine;

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.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.student.R;
import com.cooleshow.student.adapter.TeachableInstrumentAdapter;
import com.cooleshow.student.bean.TeachableInstrumentBean;
import com.cooleshow.student.contract.TeachableInstrumentContract;
import com.cooleshow.student.databinding.ActivityTeachableInstrumentBinding;
import com.cooleshow.student.presenter.mine.TeachableInstrumentPresenter;

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

import io.rong.imkit.utils.StatusBarUtil;

/**
 * 创建日期：2022/6/1 9:15
 *
 * @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);
                            }
                        }
                    }
                }
                if (selectTeachableInstrument.size() == 0) {
                    ToastUtil.getInstance().showShort("请选择乐器");
                    break;
                }
                presenter.setSubject(selectTeachableInstrument.get(0).id + "");

                break;
        }
    }


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

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        hintBar = getIntent().getBooleanExtra("hintBar", false);
        if (hintBar) {
            viewBinding.llTop.setVisibility(View.GONE);
            viewBinding.viewBar.setVisibility(View.VISIBLE);
        } else {
            viewBinding.viewBar.setVisibility(View.GONE);
            initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "乐器选择");
        }

        instrumentList = viewBinding.rvInstrument;
        viewBinding.tvConfirm.setOnClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        instrumentList.setLayoutManager(manager);
        teachableInstrumentAdapter = new TeachableInstrumentAdapter(dataList);
        instrumentList.setAdapter(teachableInstrumentAdapter);
        initCarryData();
    }

    @Override
    public void onBackPressed() {
        if (!hintBar) {
            super.onBackPressed();
        }
    }

    private TeachableInstrumentBean carryList = null;

    private void initCarryData() {
        if (null != myBundle) {
            carryList = (TeachableInstrumentBean) myBundle.getSerializable("selectTeachableInstrument");
        }

    }

    @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) {
                        if (null != carryList) {
                            if (carryList.id == subject.id) {
                                subject.isSelect = true;
                            }
                        }
                    }
                    dataList.add(datum);
                }

            }
        }


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

    }

    @Override
    public void setSubjectSuccess(Object o) {
        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.get(0));
        setResult(RESULT_OK, intent);
        finish();
    }
}
