package com.example.bpa.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import androidx.annotation.NonNull;

import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.example.bpa.R;
import com.example.bpa.view.adapter.lc_item_adapter;

import java.util.ArrayList;
import java.util.List;


public class lc_dialog extends Dialog implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    private final LayoutInflater mLayoutInflater;
    Spinner mSpinner;
    Button mConfirm,mCancel;
    List<String> mDataList = new ArrayList<String>();

    private View.OnClickListener cancelListener, confirmListener;


//    public lc_dialog setsMessage(){
//
//    }

    public lc_dialog( Context context,List<String> list ) {
        super(context);
        this.mLayoutInflater = LayoutInflater.from(context);
        mDataList = list;
    }


    public lc_dialog setsConfirm( View.OnClickListener listener) {
        this.confirmListener = listener;
        return this;
    }

    public lc_dialog setsCancel(View.OnClickListener listener) {

        this.cancelListener = listener;
        return this;
    }



    // TODO: Rename and change types and number of parameters
//    public static lc_dialog newInstance() {
//        lc_dialog fragment = new lc_dialog();
//        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
//        fragment.setArguments(args);
//        return fragment;
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lc_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);

        //自定义Dialog宽度
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) ((size.x)*0.7);        //设置为屏幕的0.7倍宽度
        getWindow().setAttributes(p);

        init();
    }


    private void init() {
        mSpinner = findViewById(R.id.material_select);
        List<String> m = new ArrayList<String>();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, android.R.id.text1, m);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
        mCancel = findViewById(R.id.cancel);
        mConfirm = findViewById(R.id.confirm);

        mConfirm.setOnClickListener(this);
        mCancel.setOnClickListener(this);

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.confirm:
                if(confirmListener != null){
                    confirmListener.onClick(v);
                }
                break;
            case R.id.cancel:
                if(cancelListener != null){
                    cancelListener.onClick(v);
                }
                break;
        }
    }


}


