package xn.hxp.ui.plan.change;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.viewbinding.ViewBinding;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.rc.core.ui.activity.BaseActivity;
import com.rc.httpcore.HttpConfig;
import com.rc.httpcore.client.HttpTool;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

import xn.hxp.app.ChemicalApp;
import xn.hxp.databinding.ActivityChangeCabinetBinding;
import xn.hxp.ui.plan.FragmentCallBack;
import xn.hxp.ui.plan.room.RoomTool;
import xn.hxp.ui.plan.room.bean.locker.CabinetLayerSelect;
import xn.hxp.ui.plan.room.bean.locker.HxpCabinetVo;
import xn.hxp.ui.plan.room.dao.HxpLayerDAO;

/**
 * 更换位置
 */
public class ChangeCabinetActivity extends BaseActivity {
    private ActivityChangeCabinetBinding binding;
    private ChangeCabinetAdapter changeCabinetAdapter;
    private CabinetLayerSelect cabinetLayerSelect;

    @Override
    protected void onInit() {
        List<HxpCabinetVo> hxpCabinetVoList = RoomTool.getInstance().hxpCabinetDAO().getAll();
        changeCabinetAdapter = new ChangeCabinetAdapter(this, hxpCabinetVoList, fragmentCallBack);
        binding.cabinetVP.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.countTV.setText((position + 1) + "/" + changeCabinetAdapter.getItemCount());
            }
        });
        binding.nextBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                binding.cabinetVP.setCurrentItem(binding.cabinetVP.getCurrentItem() + 1, true);
            }
        });
        binding.lastBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                binding.cabinetVP.setCurrentItem(binding.cabinetVP.getCurrentItem() - 1, true);
            }
        });
        binding.cabinetVP.setAdapter(changeCabinetAdapter);

        binding.cancelBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                finish();
            }
        });

        binding.confirmBT.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                if (cabinetLayerSelect != null) {
                    HxpLayerDAO hxpLayerDAO = RoomTool.getInstance().hxpLayerDAO();
                    hxpLayerDAO.insert(cabinetLayerSelect);
                    hxpLayerDAO.change(cabinetLayerSelect.getCabinetId(), cabinetLayerSelect.getDoorId(), cabinetLayerSelect.getLayer());
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("Change", cabinetLayerSelect);
                    intent.putExtra("data", bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    showToast("请选择要更换的柜层!");
                }
            }
        });

        try {
            // 日期
            binding.nowTime.setText(TimeUtils.getNowString(new SimpleDateFormat("MM月dd日 EEEE ", Locale.getDefault())));
            // 用户
            Glide.with(this).load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.userData.getAvatar()).into(binding.imageName);
            String userName = ChemicalApp.userData.getUserName();
            binding.tvName.setText(TextUtils.isEmpty(userName) ? "" : userName);
            // 实验室logo
            Glide.with(this).load(HttpConfig.Companion.getAPI_BASE_IMG_URL() + ChemicalApp.confs.getCircularLogo()).into(binding.image);
            // 实验室名称
            String subName = ChemicalApp.subjectName;
            subName = TextUtils.isEmpty(subName) ? "" : subName;
            // 房间号
            String roomNum = ChemicalApp.subRoom;
            roomNum = TextUtils.isEmpty(roomNum) ? "" : roomNum;
            binding.deptName.setText(subName + "-" + roomNum);
        } catch (Exception e) {
            LogUtils.e(Log.getStackTraceString(e));
        }
        // 退出
        binding.tvOutLogin.setOnClickListener(new ClickUtils.OnDebouncingClickListener() {
            @Override
            public void onDebouncingClick(View v) {
                try {
                    HttpTool.logout();
                } catch (IOException e) {
                    LogUtils.e(Log.getStackTraceString(e));
                }
            }
        });
    }

    FragmentCallBack fragmentCallBack = new FragmentCallBack() {
        @Override
        public void onSelected(CabinetLayerSelect lastSelectInfo) {
            cabinetLayerSelect = lastSelectInfo;
        }
    };

    @Override
    protected void cdFinish() {
        finish();
    }

    @Override
    protected void cdTime(int cd) {

    }

    @Override
    protected ViewBinding setViewBinding() {
        return binding = ActivityChangeCabinetBinding.inflate(getLayoutInflater());
    }
}