package com.bonait.bnframework.modules.home.fragment.mode;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSTYPE;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.utils.DisplayManager;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.databinding.DialogAddGoodInfoBinding;
import com.bonait.bnframework.event.ImageUrlSelectEvent;
import com.bonait.bnframework.modules.home.fragment.from.ImageChooseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.ButterKnife;

/**
 * @author: liup
 * @description: 添加商品
 * @date: 2024/6/7 16:02.
 */
public class AddGoodDialog extends DialogFragment {

    private DialogAddGoodInfoBinding viewBinding;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.DialogScale);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = DialogAddGoodInfoBinding.inflate(inflater,container,false);
        ButterKnife.bind(this, viewBinding.getRoot());
        DisplayManager.scaleViewGroup(viewBinding.getRoot());
        return viewBinding.getRoot();
    }



    @Override
    public void onStart() {
        super.onStart();
        if(getDialog()!=null){
            Window window = getDialog().getWindow();
            assert window != null;
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.CENTER;
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = ViewGroup.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
        }
        initView();
        initData();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    public String url="";

    private void initView(){
        viewBinding.closeFrom.setOnClickListener(v->{
            dismiss();
        });
        viewBinding.save.setOnClickListener(v->{
            String name = viewBinding.edittextName.getText().toString();
            if(name.isEmpty()){
                ToastUtils.info("名称不能为空");
                return;
            }
            ArrayList<BPA_GOODS> goods = QueryDB.GetGoodsALL();
            boolean isSucess = QueryDB.GetGoodsIs(name);
            if(isSucess)
            {
                ToastUtils.info("菜谱名称已存在!");
                return;
            }
            BPA_GOODS Data=new BPA_GOODS();
            Data.name = name;
            Data.status = 1;
            Data.sort = goods.size() + 1;
            Data.maketime = 180;
            Data.issc = 1;
            Data.url = url;
            Data.foreignKeyRe=viewBinding.edittextFk.getText().toString();
            Data.goodtype=mapTypes.get(viewBinding.spinner.getSelectedItem().toString());
            QueryDB.AddGoods(Data);
            if(saveCallBack!=null){
                saveCallBack.onSave(mapTypes.get(viewBinding.spinner.getSelectedItem().toString()));
            }
        });
        viewBinding.image.setOnClickListener(v->{
            Intent intent = new Intent(getContext(), ImageChooseActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });
    }

    Map<String,String> mapTypes = new LinkedHashMap<>();
    private void initData(){
        url = "";
        for (BPA_GOODSTYPE item:QueryDB.GetGoodsTypeALL())
        {
            mapTypes.put(item.name,item.id);
        }
        initSpinner();
    }


    private void initSpinner()
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_text32_item, new ArrayList<>(mapTypes.keySet()));
        adapter.setDropDownViewResource(R.layout.spinner_dropdown32_item);
        viewBinding.spinner.setAdapter(adapter);
    }
    private SaveCallBack saveCallBack;

    public void setSaveCallBack(SaveCallBack saveCallBack) {
        this.saveCallBack = saveCallBack;
    }

    public interface SaveCallBack{
        void onSave(String typeId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //接收图片选择事件
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onImageSelectEvent(ImageUrlSelectEvent event){
        url = event.url;
        new MyBitmapUtils().disPlay(viewBinding.image,event.url);
    }
}
