package com.cooleshow.teacher.ui.live;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
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.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.teacher.R;
import com.cooleshow.teacher.contract.CreateLiveContract;
import com.cooleshow.teacher.databinding.ActivityCreateLiveBinding;
import com.cooleshow.teacher.presenter.live.CreateLivePresenter;

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

import androidx.annotation.Nullable;

/**
 * 创建日期：2022/5/20 14:45
 *
 * @author Ryan
 * 类说明：
 */
@Route(path = RouterPath.LiveCenter.TEACHER_MINE_CREATE_COURSE)
public class CreateLiveActivity extends BaseMVPActivity<ActivityCreateLiveBinding, CreateLivePresenter> implements CreateLiveContract.CreateLiveView, View.OnClickListener {
    public static final String[] LIVE_TIMES_LIMIT = new String[]{"30", "45", "60", "90"};
    private OptionsPickerView pvOptions;
    private List<String> mTimes;
    private String currentLimitTime = LIVE_TIMES_LIMIT[0];

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @Override
    protected void initView() {
        initMidTitleToolBar(viewBinding.toolbarInclude.toolbar, "创建直播");
        viewBinding.tvConfirm.setOnClickListener(this);
        viewBinding.csLiveDuration.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        mTimes = Arrays.asList(LIVE_TIMES_LIMIT);

    }

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

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

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_confirm) {
            //创建直播间
            String liveTitle = viewBinding.etTitle.getText().toString().trim();
            if (TextUtils.isEmpty(liveTitle)) {
                ToastUtil.getInstance().showShort("请输入直播间标题");
                return;
            }
            String liveContent = viewBinding.etContent.getText().toString().trim();
            if (TextUtils.isEmpty(liveContent)) {
                ToastUtil.getInstance().showShort("请输入直播间内容");
                return;
            }
            presenter.createTempLive(liveContent, liveTitle,currentLimitTime);
            return;
        }

        if (view.getId() == R.id.cs_live_duration) {
            showTimeSelect();
            return;
        }
    }


    private void showTimeSelect() {
        if (pvOptions == null) {
            pvOptions = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
                currentLimitTime = mTimes.get(options1);
                viewBinding.tvLiveTime.setText(currentLimitTime + "分钟");
                viewBinding.tvLiveTime.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_333333));
            }).setLayoutRes(R.layout.pickerview_live_time_layout, v -> {
                //自定义布局中的控件初始化及事件处理
                final TextView tvSubmit = (TextView) v.findViewById(com.cooleshow.base.R.id.tv_finish);
                TextView ivCancel = (TextView) v.findViewById(com.cooleshow.base.R.id.tv_cancel);
                tvSubmit.setOnClickListener(v12 -> {
                    pvOptions.returnData();
                    pvOptions.dismiss();
                });
                ivCancel.setOnClickListener(v1 -> pvOptions.dismiss());

            }).isDialog(false).build();
        }
        pvOptions.setPicker(mTimes);
        pvOptions.show();

    }

    /**
     * 创建直播间成功,返回直播间id
     *
     * @param roomId
     */
    @Override
    public void onCreateLiveSuccess(String roomId) {
        ToastUtil.getInstance().showShort("创建成功");
        TeacherLiveRoomActivity.start(this, roomId);
        finish();
    }
}
