package com.example.tyre.maintenance.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;

import com.example.tyre.R;
import com.example.tyre.databinding.DialogTireUpdateBinding;
import com.example.tyre.maintenance.adapter.DisposalFileAdapter;
import com.example.tyre.maintenance.been.BaseTyre;
import com.example.tyre.maintenance.been.BizOrderTireDetail;
import com.example.tyre.maintenance.been.CheckInstanceFiles;

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

public class TireUpdateDialog extends Dialog implements TireChangeDialog.TireChangeListener {
    private DialogTireUpdateBinding binding;
    private BaseTyre baseTyre;
    private ObservableBoolean isupdate;
    private BizOrderTireDetail bizOrderTireDetail;
    private BizOrderTireDetail newTireDetail;// 新轮胎
    private TireUpdateDialogSaveListener listener;
    private File image;
    // private ObservableBoolean photoState;
    private TireChangeDialog tireChangeDialog;// 同车轮胎互换弹窗
    private List<BaseTyre> baseTyreList;
    private BaseTyre newBaseTyre;
    private Context context;
    private DisposalFileAdapter disposalFileAdapter;
    private List<CheckInstanceFiles> filesList;

    public TireUpdateDialog(@NonNull Context context, TireUpdateDialogSaveListener listener) {
        super(context, R.style.MyMiddleDialogStyle);
        this.context = context;
        this.listener = listener;
        binding =
                DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_tire_update, null, false);
        isupdate = new ObservableBoolean(false);
        binding.setIsUpdate(isupdate);
        bizOrderTireDetail = new BizOrderTireDetail();
        binding.dialogTitle.setOnClickListener(view -> dismiss());

        binding.dialogIsupdateSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            isupdate.set(isChecked);
        });

// 保存按钮点击事件
        binding.dialogTireUpdateSave.setOnClickListener(view -> {
            String shendu = binding.dialogTireShendu1.getText().toString();
            String taiya = binding.dialogTireTaiya1.getText().toString();
            if (shendu.isEmpty()) {
                Toast.makeText(context, "请输入花纹深度", Toast.LENGTH_SHORT).show();
                return;
            }
            // if (image == null) {
            //     Toast.makeText(context, "请上传轮胎照片", Toast.LENGTH_SHORT).show();
            //     return;
            // }
            bizOrderTireDetail.setRemark(binding.dialogTireRemark1.getText().toString());
            bizOrderTireDetail.setTreadDepth(shendu);// 花纹深度
            bizOrderTireDetail.setTirePress(taiya);// 胎压
            bizOrderTireDetail.setTirePressOld(binding.dialogTireTaiyaOld.getText().toString());
            boolean isupdate1 = isupdate.get();

            if (isupdate1) {
                String s = binding.newTireInfo2.getText().toString();
                String s1 = binding.dialogTireShendu2.getText().toString();
                String s2 = binding.dialogTireTaiya2.getText().toString();
                if (s1.isEmpty()) {
                    Toast.makeText(context, "请输入花纹深度", Toast.LENGTH_SHORT).show();
                    return;
                }
                newTireDetail.setTreadDepth(s1);// 花纹深度
                newTireDetail.setTirePress(s2);// 胎压
                bizOrderTireDetail.setDataType("换新胎，卸车");
                bizOrderTireDetail.setTireStatus("换新胎，卸车");
            }
            listener.onSave(bizOrderTireDetail, image, isupdate1, newTireDetail);
        });
        // 同车互换
        binding.dialogButtonTong.setOnClickListener(view -> {
            // listener.onChengeTong(baseTyre.getWheelPostion());
            tireChangeDialog.setBaseTyreList(baseTyreList);
            tireChangeDialog.setLocationOld(baseTyre.getWheelPostion());
            tireChangeDialog.show();
        });


        // 拍照按钮点击事件
        binding.dialogUpdatePhoto.setOnClickListener(view -> listener.onTakePhoto());
        // photoState = new ObservableBoolean(false);
        // binding.setPhotoState(photoState);
        setContentView(binding.getRoot());
        tireChangeDialog = new TireChangeDialog(context,this);
        disposalFileAdapter = new DisposalFileAdapter(context, new DisposalFileAdapter.ItemClickCall() {
            @Override
            public void onClick(int position, boolean type) {
                if (!type) {
                    filesList.remove(position);
                    disposalFileAdapter.notifyDataSetChanged();
                }
            }
        });
        filesList = new ArrayList<>();
        disposalFileAdapter.setList(filesList);
        binding.setAdapter1(disposalFileAdapter);
    }

    // 换新胎了
    public void setNewBaseTyre(BaseTyre newBaseTyre) {
        this.newBaseTyre = newBaseTyre;

        // 显示用
        String tyreNo = baseTyre.getTyreNo();
        String selfNo = baseTyre.getSelfNo();
        binding.newTireInfo1.setText(tyreNo == null ? baseTyre.getTyreEpc() : tyreNo);
        binding.newTireInfo2.setText(selfNo);
        binding.dialogTireShendu2.setText(newBaseTyre.getPatternDepth());
        //     id
        Long tyreId = baseTyre.getTyreId();
        newTireDetail.setTireId(tyreId);
        newTireDetail.setTireCode(tyreNo);
        newTireDetail.setBaseTyre(newBaseTyre);
    }

    public void setBaseTyreList(List<BaseTyre> baseTyreList) {
        this.baseTyreList = baseTyreList;
    }

    public void setBaseTyre(BaseTyre baseTyre, BizOrderTireDetail tag) {
        this.baseTyre = baseTyre;
        binding.setTire(baseTyre);
        bizOrderTireDetail = new BizOrderTireDetail();
        bizOrderTireDetail.setTireId(baseTyre.getTyreId());
        bizOrderTireDetail.setTireCode(baseTyre.getTyreNo());
        bizOrderTireDetail.setPositionName(baseTyre.getWheelPostion());
        bizOrderTireDetail.setDataType("保养");
        bizOrderTireDetail.setTireStatus("保养");
        bizOrderTireDetail.setBaseTyre(baseTyre);
        // 换新胎初始化
        newTireDetail = new BizOrderTireDetail();
        newTireDetail.setPositionName(baseTyre.getWheelPostion());// 保存轮位
        newTireDetail.setDataType("换新胎，装车");
        newTireDetail.setTireStatus("换新胎，装车");
        // 重置ui
        // photoState.set(false);
        binding.dialogUpdatePhoto.setImageResource(R.mipmap.ic_xiangji);
        if (tag != null) {
            binding.dialogTireRemark1.setText(tag.getRemark());
            binding.dialogTireShendu1.setText(tag.getTreadDepth());
            // 花纹深度
            binding.dialogTireTaiya1.setText(tag.getTirePress());// 胎压
            binding.dialogTireTaiyaOld.setText(tag.getTirePressOld());// 胎压
        } else {
            binding.dialogTireRemark1.setText(null);
            binding.dialogTireShendu1.setText(null);
            ;// 花纹深度
            binding.dialogTireTaiya1.setText(null);// 胎压
            binding.dialogTireTaiyaOld.setText(null);// 胎压
        }

        show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDialogStyle();
    }

    /**
     * 设置弹窗样式（匹配原型：大圆角、居中、占屏比例）
     */
    private void initDialogStyle() {
        Window window = getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams params = window.getAttributes();
            DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
            params.width = displayMetrics.widthPixels;
            params.height = (int) (displayMetrics.heightPixels * 0.9);
            window.setAttributes(params);
            window.setBackgroundDrawableResource(android.R.color.transparent);

        }
        // 点击外部不关闭
        setCanceledOnTouchOutside(false);
    }

    public void setImage(String fileUrl, File image) {
        this.image = image;
        // photoState.set(true);
        // binding.dialogUpdatePhoto.setImageResource(R.mipmap.ic_duihao);
        CheckInstanceFiles checkInstanceFiles = new CheckInstanceFiles();
        checkInstanceFiles.setFaultFile(fileUrl);
        checkInstanceFiles.setState(true);
        filesList.add(checkInstanceFiles);
        disposalFileAdapter.notifyDataSetChanged();

    }
    // 同车互换修改
    @Override
    public void onChangeSave(String oldLocation, String newLocation) {
        tireChangeDialog.   dismiss();

        listener.onChangeSave(oldLocation, newLocation);
        dismiss();
    }

    public interface TireUpdateDialogSaveListener {
        void onSave(BizOrderTireDetail bizOrderTireDetail, File image, boolean isupdate, BizOrderTireDetail newTireDetail);

        void onTakePhoto();
        void onChangeSave(String oldLocation, String newLocation);

    }


}
