package com.wisewoods.eduandroid.ui;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.tools.ScreenUtils;
import com.wisewoods.base.bean.Results;
import com.wisewoods.base.net.JDCallback;
import com.wisewoods.base.utils.$;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.app.App;
import com.wisewoods.eduandroid.app.BasePhotoActivity;
import com.wisewoods.eduandroid.databinding.ActivityStudentLeaveBinding;
import com.wisewoods.eduandroid.oss.ICloundOS;
import com.wisewoods.eduandroid.oss.OSSManager;
import com.wisewoods.eduandroid.ui.adapter.photo.FullyGridLayoutManager;
import com.wisewoods.eduandroid.ui.adapter.photo.GridImageAdapter;

import java.util.Calendar;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Response;

public class StudentLeaveActivity extends BasePhotoActivity<ActivityStudentLeaveBinding> {

    private GridImageAdapter mAdapter;
    private String id;

    public static void start(String id) {
        Intent intent = new Intent(ActivityUtils.getTopActivity(), StudentLeaveActivity.class);
        intent.putExtra("id", id);
        ActivityUtils.startActivity(intent);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        id = getIntent().getStringExtra("id");
        showBarBack(binding.bar.tvTitle, "学员请假", binding.bar.ivBack);
        binding.tvPut.setOnClickListener(v -> {
            if (isFastClick()){
                put();
            }
        });

        FullyGridLayoutManager manager = new FullyGridLayoutManager(this,
                3, GridLayoutManager.VERTICAL, false);

        binding.rvPhoto.setLayoutManager(manager);

        binding.rvPhoto.addItemDecoration(new GridSpacingItemDecoration(3,
                ScreenUtils.dip2px(this, 8), false));

        binding.tvStart.setOnClickListener(v -> {
            selectStart();
        });

        binding.tvEnd.setOnClickListener(v -> {
            selectEnd();
        });


        mAdapter = new GridImageAdapter(this, R.drawable.ic_photo_add, () -> {
            KeyboardUtils.hideSoftInput(this);
            showLoading();
            OSSManager.getInstance().selectImage(this, OSSManager.OSSPATH, (code, msg, localMedia) -> {
                if (code == ICloundOS.CODE_SUCCESS) {
                    mAdapter.getData().add(localMedia);
                    mAdapter.notifyDataSetChanged();
                }
                hideLoading();
            });
        });
        mAdapter.setSelectMax(1);
        mAdapter.setOnItemClickListener(listener);
        binding.rvPhoto.setAdapter(mAdapter);

    }


    private void put() {
        String start = binding.tvStart.getText().toString().trim();
        if (TextUtils.isEmpty(start)) {
            $.toast("请选择请假开始时间");
            return;
        }
        String end = binding.tvEnd.getText().toString().trim();
        if (TextUtils.isEmpty(end)) {
            $.toast("请选择请假结束时间");
            return;
        }

        String content = binding.etContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            $.toast("请输入请假内容");
            return;
        }

        if (mAdapter.getData().size() == 0) {
            $.toast("请上传请假证明");
            return;
        }


        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        HashMap<String, String> map = new HashMap<>();
        map.put("eStuId", id);
        map.put("startTime", start);
        map.put("endTime", end);
        map.put("leaveCause", content);
        map.put("leavePath", mAdapter.getData().get(0).getCompressPath());
        RequestBody body = RequestBody.create(GsonUtils.toJson(map), JSON);

        App.api.studentLeave(body).enqueue(new JDCallback<Results>() {
            @Override
            public void onS(Response<Results> response, Results results) {
                $.toast("提交成功");
                finish();
            }
        });
    }

    private void selectStart() {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.DATE, 1);

        TimePickerView pvTime = new TimePickerBuilder(this, (date, v12) -> {//选中事件回调
            if (TimeUtils.getTimeSpan(TimeUtils.getNowDate(), date, TimeConstants.SEC) < 0) {
                binding.tvStart.setText(TimeUtils.date2String(date));
            } else {
                $.toast("选择的时间不能小于当前时间");
            }
        })
                .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setRangDate(startDate, null)//起始终止年月日设定
                .build();
        pvTime.show();
    }

    private void selectEnd() {
        String startStr = binding.tvStart.getText().toString().trim();
        if (TextUtils.isEmpty(startStr)) {
            $.toast("请选择请假开始时间");
            return;
        }
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(TimeUtils.string2Date(startStr));

        TimePickerView pvTime = new TimePickerBuilder(this, (date, v12) -> {//选中事件回调
            if (TimeUtils.getTimeSpan(TimeUtils.getNowDate(), date, TimeConstants.SEC) < 0) {
                if (TimeUtils.getTimeSpan(startDate.getTime(), date, TimeConstants.SEC) < 0) {
                    binding.tvEnd.setText(TimeUtils.date2String(date));
                } else {
                    $.toast("结束时间必须大于开始时间");
                }
            } else {
                $.toast("选择的时间不能小于当前时间");
            }

        })
                .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
                .setRangDate(startDate, null)//起始终止年月日设定
                .build();
        pvTime.show();
    }

}
