package com.wisewoods.eduandroid.ui;

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

import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.ScreenUtils;
import com.wisewoods.base.bean.BaseEvent;
import com.wisewoods.base.bean.Results;
import com.wisewoods.base.bean.TaskBean;
import com.wisewoods.base.bean.UploadImgBean;
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.ActivityCarPlanBinding;
import com.wisewoods.eduandroid.oss.ICloundOS;
import com.wisewoods.eduandroid.oss.OSSManager;
import com.wisewoods.eduandroid.ui.adapter.ImgShowAdapter;
import com.wisewoods.eduandroid.ui.adapter.photo.FullyGridLayoutManager;
import com.wisewoods.eduandroid.ui.adapter.photo.GridImageAdapter;

import org.greenrobot.eventbus.EventBus;

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

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

public class CarPlanActivity extends BasePhotoActivity<ActivityCarPlanBinding> {

    private GridImageAdapter mAdapter;
    private String id;
    private String content;
    private int state; //0 是安排乘车 1是查看安排
    private ImgShowAdapter imgShowAdapter;
    List<LocalMedia> imgs;


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

    @Override
    protected void initData(Bundle savedInstanceState) {
        id = getIntent().getStringExtra("id");
        content = getIntent().getStringExtra("content");
        state = getIntent().getIntExtra("state", 0);
        binding.etMsg.setText(content);

        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.tvPut.setOnClickListener(v -> put());

        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.setOnItemClickListener(listener);
        binding.rvPhoto.setAdapter(mAdapter);
        String title = "";
        if (state == 0) {
            title = "安排乘车";
            binding.tvPut.setText("确定");
        } else {
            title = "查看安排";
            binding.tvPut.setText("修改");
            getData();
        }
        showBarBack(binding.bar.tvTitle, title, binding.bar.ivBack);
    }

    private void getData() {
        App.api.getTask(id).enqueue(new JDCallback<TaskBean>() {
            @Override
            public void onS(Response<TaskBean> response, TaskBean taskBean) {
                binding.etContent.setText(taskBean.data.feedbackContent);

                imgs = new ArrayList<>();
                for (UploadImgBean uploadImgBean : taskBean.data.feedbackDocumentUrl) {
                    if (PictureMimeType.isSuffixOfImage(uploadImgBean.url)) {
                        LocalMedia localMedia = new LocalMedia();
                        localMedia.setCompressed(true);
                        localMedia.setFileName(uploadImgBean.name);
                        localMedia.setCompressPath(uploadImgBean.url);
                        imgs.add(localMedia);
                    }
                }
                mAdapter.setList(imgs);
                mAdapter.notifyDataSetChanged();
            }
        });
    }


    private List<UploadImgBean> imgDatas;
    private HashMap<String, String> imgPathToNet = new HashMap<>();

    private void put() {
        String content = binding.etContent.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            $.toast("输入联系信息");
            return;
        }

        if (mAdapter.getData().size() == 0) {
            $.toast("请上传车辆照片");
            return;
        }

        imgDatas = new ArrayList<>();
        for (LocalMedia localMedia : mAdapter.getData()) {
            imgDatas.add(new UploadImgBean(localMedia.getFileName(), localMedia.getCompressPath()));
        }

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("taskStatus", "3");
        map.put("feedbackContent", content);
        map.put("feedbackDocument", GsonUtils.toJson(imgDatas));
        RequestBody body = RequestBody.create(GsonUtils.toJson(map), JSON);

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


}
