package com.bonait.bnframework.modules.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ExecuteTheRecipe;
import com.bonait.bnframework.common.constant.DataBus;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_SUBORDER;
import com.bonait.bnframework.common.db.res.MakeStatus;
import com.bonait.bnframework.common.db.res.ResGoodsMake;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.helper.MediaPlayerHelper;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class loadinggood_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    /**
     * 内部点击事件
     */
    private MyClickListener mListener;
    private ArrayList<ResGoodsMake> datas= DataBus.getInstance().GoodsMake;
    int resource1;
    public Context conmain;
    public Activity activity;

    private final LayoutInflater mLayoutInflater;


    public loadinggood_adapter(Context context,MyClickListener myClickListener,Activity ac) {
        this.conmain = context;
        activity=ac;
        mListener=myClickListener;
        mLayoutInflater = LayoutInflater.from(context);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.loadinggood_item, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            ResGoodsMake goodsMake =datas.get(position);
            // 设置要显示的图片和文字
            myViewHolder.loading_ProgressBar.setProgress(goodsMake.makeProcess);
            myViewHolder.loading_name.setText(goodsMake.good.name);
            myViewHolder.loading_status.setText(goodsMake.makeStatus.name());
            myViewHolder.loading_zuofa.setText(goodsMake.subOrder.exp);

            DecimalFormat decimalFormat =new DecimalFormat("00");
            myViewHolder.loading_xuhao.setText(decimalFormat.format(position+1));
//            myViewHolder.loading_time.setText(new SimpleDateFormat("HH:mm:ss").format(Date.parse(goodsMake.subOrder.createTime)));
            switch (goodsMake.makeStatus.name())
            {
                case "等待中":
                    //myViewHolder.quxiaozhizuo.setVisibility(View.GONE);
                    //myViewHolder.loading_status.setTextColor(conmain.getResources().getColor(R.color.text4));
                    break;
                case "制作中":
                   // myViewHolder.quxiaozhizuo.setVisibility(View.VISIBLE);//取消制作
                    myViewHolder.loading_status.setTextColor(conmain.getResources().getColor(R.color.green_primary_dark));
                    break;
                case "制作完成":
                   // myViewHolder.quxiaozhizuo.setVisibility(View.VISIBLE);//取消制作
                    //myViewHolder.loading_status.setTextColor(conmain.getResources().getColor(R.color.tab_text_normal));
                    break;
            }
            myViewHolder.loading_main.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mListener!=null) mListener.clickListener(view,goodsMake);
                }
            });

            myViewHolder.quxiaozhizuo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String title = "温馨提示！";
                    String message = "客官确定要取消制作【"+goodsMake.good.name+"】吗？";
                    AlertDialogUtils.showDialog(conmain, title, message, new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            DataBus.getInstance().DeleteGoodsMake(goodsMake.subOrder.id);
                            ToastUtils.info("取消订单制作成功！");
                            dialog.dismiss();
                        }
                    });

                }
            });
        }
    }

    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }
    /**
     * 刷新
     *
     * @param
     */
    public void refresh() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    notifyDataSetChanged();
                } catch (Exception e) {

                }
            }
        });
    }

    /**
     * 播放
     * @param resid
     */
    public void Speak(int resid)
    {
        MediaPlayerHelper.getInstance().PlaySound(resid);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout loading_main;
        ProgressBar loading_ProgressBar;
        TextView loading_name;
        TextView loading_zuofa;
        TextView loading_status;
        TextView loading_time;
        TextView loading_xuhao;
        Button quxiaozhizuo;
        public MyViewHolder(View view) {
            super(view);
            loading_main = view.findViewById(R.id.loading_main);
            loading_ProgressBar = view.findViewById(R.id.loading_ProgressBar);
            loading_name = view.findViewById(R.id.loading_name);
            loading_zuofa = view.findViewById(R.id.loading_zuofa);
            loading_status = view.findViewById(R.id.loading_status);
            loading_time= view.findViewById(R.id.loading_time);
            quxiaozhizuo= view.findViewById(R.id.quxiaozhizuo);

            loading_xuhao= view.findViewById(R.id.loading_xuhao);
        }
    }
}