package com.unibuy.smartdevice.ui.devs.electric;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.unibuy.smartdevice.databinding.RecyclerElectricSlotListBinding;
import com.unibuy.smartdevice.exception.Logs;
import com.unibuy.smartdevice.structure.ProductStructure;
import com.unibuy.smartdevice.structure.SlotStructure;
import com.unibuy.smartdevice.tools.Tools;
import com.unibuy.smartdevice.ui.ProductSearchActivity;

import java.util.List;

public class RecyclerElectricSlotListAdpter extends RecyclerView.Adapter<RecyclerElectricSlotListAdpter.ViewHolder> {
    private Logs logs;
    private Context context;
    private Tools tools;
    private final ActivityResultLauncher<Intent> resultLauncher;
    private final List<SlotStructure> slotList;

    public RecyclerElectricSlotListAdpter(Context context, ActivityResultLauncher<Intent> resultLauncher, List<SlotStructure> slotList) {
        this.logs = new Logs(this.getClass());
        this.context = context;
        this.tools = new Tools(context, logs);
        this.resultLauncher = resultLauncher;
        this.slotList = slotList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerElectricSlotListBinding binding;

        public ViewHolder(@NonNull RecyclerElectricSlotListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 使用 ViewBinding 來綁定佈局
        RecyclerElectricSlotListBinding binding = RecyclerElectricSlotListBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);

        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SlotStructure slot = slotList.get(position);
        holder.binding.textSlot.setText(String.valueOf(slot.getSlot()));
        String productID = slot.getProduct().getProductID();
        if (!productID.isEmpty() && !productID.equals("?")) {
            holder.binding.textProductName.setText(slot.getProduct().getProductName());
        } else {
            holder.binding.textProductName.setText("點我選擇商品");
        }
        holder.binding.textCount.setText(String.valueOf(slot.getCount()));
        holder.binding.editCount.setText(String.valueOf(slot.getCount()));
        if (slot.isLock()) {
            holder.binding.textLock.setText("鎖定");
            holder.binding.textLock.setTextColor(Color.BLACK);
        } else {
            holder.binding.textLock.setText("未鎖定");
            holder.binding.textLock.setTextColor(Color.GRAY);
        }
        holder.binding.textProductName.setOnClickListener(new selectPositionOnClickListener(position));
        holder.binding.textCount.setOnClickListener(new textCountOnClickListener(holder.binding, slot.getCount()));
        holder.binding.textLock.setOnClickListener(new textLockOnClickListener(position));
        holder.binding.textDelete.setOnClickListener(new textDeleteOnClickListener(position));
        holder.binding.editCount.setOnEditorActionListener(new editCountOnEditorActionListener(holder.binding, position));
        holder.binding.editCount.setOnClickListener(new editCountOnClickListener(holder.binding, slot.getCount()));

        tools.setImmHideByFocusChange(holder.binding.editCount);
    }

    public class editCountOnClickListener implements View.OnClickListener {
        private RecyclerElectricSlotListBinding binding;
        private int count;

        public editCountOnClickListener(RecyclerElectricSlotListBinding binding, int count) {
            this.binding = binding;
            this.count = count;
        }

        @Override
        public void onClick(View v) {
            if (this.count == 0) {
                binding.editCount.setText("");
            }
        }
    }

    public class selectPositionOnClickListener implements View.OnClickListener{
        private int position;

        public selectPositionOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ProductSearchActivity.class);
            intent.putExtra("position", position);
            resultLauncher.launch(intent);
        }
    }

    public class textCountOnClickListener implements ViewPager.OnClickListener{
        private RecyclerElectricSlotListBinding binding;
        private int count;

        public textCountOnClickListener(RecyclerElectricSlotListBinding binding, int count) {
            this.binding = binding;
            this.count = count;
        }

        @Override
        public void onClick(View v) {
            binding.textCount.setVisibility(View.GONE);
            binding.editCount.setVisibility(View.VISIBLE);
            if (this.count == 0) {
                binding.editCount.setText("");
            }
            tools.setImmOpenByFocusChange(binding.editCount);
        }
    }

    public class editCountOnEditorActionListener implements TextView.OnEditorActionListener{
        private RecyclerElectricSlotListBinding binding;
        private int position;

        public editCountOnEditorActionListener(RecyclerElectricSlotListBinding binding, int position) {
            this.binding = binding;
            this.position = position;
        }

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEND) {
                int count = 0;
                if (v.getText().length() > 0) {
                    count = Integer.valueOf(v.getText().toString());
                }
                slotList.get(position).setCount(count);
                binding.textCount.setText(String.valueOf(count));
                binding.editCount.setVisibility(View.GONE);
                binding.textCount.setVisibility(View.VISIBLE);
                notifyItemChanged(position);
                tools.hideSystemBars((Activity) context);
                return true;
            }
            return false;
        }
    }

    public class textLockOnClickListener implements View.OnClickListener{
        private int position;

        public textLockOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            setProductDataIsLock(position);
        }
    }

    public class textDeleteOnClickListener implements View.OnClickListener{
        private int position;

        public textDeleteOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            delProductData(position);
        }
    }
    @Override
    public int getItemCount() {
        return slotList.size();
    }

    public void addProductData(SlotStructure productStructure) {
        slotList.add(productStructure);
        notifyItemInserted(slotList.size() -1);
    }

    public void setProductDataIsLock(int position) {
        slotList.get(position).setLock(slotList.get(position).isLock()?false:true);
        notifyItemChanged(position);
    }

    public void delProductData(int position) {
        slotList.get(position).setProduct(new ProductStructure("?", "", "", 0, 0, 0, 0, "", "","",""));
        slotList.get(position).setCount(0);
        slotList.get(position).setLock(false);
        notifyItemChanged(position);
    }
}
