package com.example.tyre.maintenance.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
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 com.example.tyre.R;
import com.example.tyre.databinding.DialogTireChangeBinding;
import com.example.tyre.maintenance.been.BaseTyre;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TireChangeDialog extends Dialog {
    private DialogTireChangeBinding binding;
    private List<BaseTyre> baseTyreList;
    private Map<String, BaseTyre> tyreMap;
    private String locationOld;
    private TireChangeListener tireChangeListener;
    private String newLocation;

    public TireChangeDialog(@NonNull Context context, TireChangeListener tireChangeListener) {
        super(context, R.style.MyMiddleDialogStyle);
        this.tireChangeListener = tireChangeListener;
        binding =
                DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_tire_change, null, false);
        binding.dialogTitle1.setOnClickListener(view -> dismiss());
        binding.dialogChangeButton.setOnClickListener(view -> {
            tireChangeListener.   onChangeSave(locationOld,newLocation);
        });
        setContentView(binding.getRoot());
    }

    public void setLocationOld(String locationOld) {
        this.locationOld = locationOld;
    }

    public void setBaseTyreList(List<BaseTyre> baseTyreList) {
        this.baseTyreList = baseTyreList;
        tyreMap = new HashMap<>();
        baseTyreList.forEach(tire -> {
            String wheelPostion = tire.getWheelPostion();
            tyreMap.put(wheelPostion, tire);
            // if (tire.getTyreNo() == null || tire.getTyreNo().equals("")) {
            //     tire.setTyreNo("测试标识:" + wheelPostion);
            // }
            initTire(wheelPostion, tire);
        });


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDialogStyle();
        binding.tireClick1.layoutTireInfo.setOnClickListener(view -> {
            tireLayoutCllick("左前轮");
        });
        binding.tireClick2.layoutTireInfo.setOnClickListener(view -> {
            tireLayoutCllick("右前轮");
        });
        binding.tireClick3.layoutTireInfo.setOnClickListener(view -> {
            tireLayoutCllick("左外轮");
        });
        binding.tireClick4.layoutTireInfo.setOnClickListener(view -> {
            tireLayoutCllick("左内轮");
        });
        binding.tireClick5.layoutTireInfo.setOnClickListener(view -> {
            tireLayoutCllick("右内轮");
        });
        binding.tireClick6.layoutTireInfo.setOnClickListener(view -> {
            tireLayoutCllick("右外轮");
        });

    }

    private void tireLayoutCllick(String location) {
        if (location.equals(this.locationOld)) {
            Toast.makeText(getContext(), "当前轮胎位置与选择位置相同", Toast.LENGTH_SHORT).show();
            return;
        }
        this.newLocation = location;
        BaseTyre baseTyre = tyreMap.get(location);
        BaseTyre baseTyreOld = tyreMap.get(this.locationOld);
        if (baseTyre == null) {
            Toast.makeText(getContext(), "当前轮胎位置不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        initTire(this.locationOld, baseTyre);
        Log.e("TAG", "tireLayoutCllick:" + location + "," + baseTyre.toString());

        initTire(location, baseTyreOld);
        Log.e("TAG", "tireLayoutCllick:" + location + "," + baseTyreOld.toString());

        Toast.makeText(getContext(), "互换成功", Toast.LENGTH_SHORT).show();

    }

    /**
     * 设置弹窗样式（匹配原型：大圆角、居中、占屏比例）
     */
    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 = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
            window.setBackgroundDrawableResource(android.R.color.transparent);

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

    // 初始化轮胎信息
    private void initTire(String wheelPostion, BaseTyre baseTyre) {

        switch (wheelPostion) {
            case "左前轮":
                binding.setTire1(baseTyre);
                break;
            case "右前轮":
                binding.setTire2(baseTyre);
                break;
            case "左外轮":
                binding.setTire3(baseTyre);
                break;
            case "左内轮":
                binding.setTire4(baseTyre);
                break;

            case "右内轮":
                binding.setTire5(baseTyre);
                break;
            case "右外轮":
                binding.setTire6(baseTyre);
                break;
        }
    }

    public interface TireChangeListener {
        void onChangeSave(String oldLocation, String newLocation);

    }
}
