package com.zztrapp.zztrapp.ui.index.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.SparseBooleanArray;
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.AbsListView;

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

import com.zztrapp.zztrapp.R;
import com.zztrapp.zztrapp.bean.ChoosePopItemBean;
import com.zztrapp.zztrapp.databinding.FragmentChooseSingleDialogBinding;
import com.zztrapp.zztrapp.ui.index.adapter.CommonAdapter;
import com.zztrapp.zztrapp.ui.index.adapter.ViewHolder;
import com.zztrapp.zztrapp.ui.index.callback.ChooseResultCallBack;
import com.zztrapp.zztrapp.utilcode.util.LogUtils;

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

/**
 * Describe ChooseListFragment
 * Created by Bby on 2021/10/6
 * Email bby0856@163.com
 */
public class ChooseListFragment extends DialogFragment {

    private Context mContext;
    private int mChooseType;
    private String title = "";
    private List<ChoosePopItemBean> mOriginList;
    private List<ChoosePopItemBean> mChooseList;
    private ChooseResultCallBack mChooseResultCallBack;
    private FragmentChooseSingleDialogBinding binding;

    public ChooseListFragment() {
        // Required empty public constructor
    }

    /**
     * @param mContext
     * @param chooseType           单选/多选
     * @param originList           源数据
     * @param chooseList           选中数据
     * @param chooseResultCallBack 回调监听
     */
    public ChooseListFragment(Context mContext, int chooseType, List<ChoosePopItemBean> originList, List<ChoosePopItemBean> chooseList, ChooseResultCallBack chooseResultCallBack) {
        this.mContext = mContext;
        this.mChooseType = chooseType;
        this.mOriginList = originList;
        this.mChooseList = chooseList;
        this.mChooseResultCallBack = chooseResultCallBack;
    }

    public ChooseListFragment(Context mContext, String title, int chooseType, List<ChoosePopItemBean> originList, List<ChoosePopItemBean> chooseList, ChooseResultCallBack chooseResultCallBack) {
        this.mContext = mContext;
        this.title = title;
        this.mChooseType = chooseType;
        this.mOriginList = originList;
        this.mChooseList = chooseList;
        this.mChooseResultCallBack = chooseResultCallBack;
    }

    @Override
    public void onStart() {
        super.onStart();
        // 下面这些设置必须在此方法(onStart())中才有效

        Window window = getDialog().getWindow();
        // 如果不设置这句代码, 那么弹框就会与四边都有一定的距离
        window.setBackgroundDrawableResource(android.R.color.transparent);
        // 设置动画
        window.setWindowAnimations(R.style.bottomDialog);

        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 如果不设置宽度,那么即使你在布局中设置宽度为 match_parent 也不会起作用
        params.width = getResources().getDisplayMetrics().widthPixels;
        window.setAttributes(params);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        // 去掉默认的标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = FragmentChooseSingleDialogBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

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

    private void initData() {
        binding.title.setText(title);


        CommonAdapter<ChoosePopItemBean> gvSingleChooseAdapter = null;
        if (mChooseType == AbsListView.CHOICE_MODE_SINGLE) {
            gvSingleChooseAdapter = new CommonAdapter<ChoosePopItemBean>(mContext, R.layout.item_lv_tick_choose_view) {
                @Override
                public void convert(ViewHolder helper, ChoosePopItemBean item, int position, View convertView) {
                    if (item != null) {
                        helper.setText(R.id.recycle_text, item.getName());
                    }
                }
            };
            binding.gvChooseSingleView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
            binding.gvChooseSingleView.setNumColumns(1);

        } else if (mChooseType == AbsListView.CHOICE_MODE_MULTIPLE) {

            gvSingleChooseAdapter = new CommonAdapter<ChoosePopItemBean>(mContext, R.layout.item_lv_box_choose_view) {
                @Override
                public void convert(ViewHolder helper, ChoosePopItemBean item, int position, View convertView) {
                    if (item != null) {
                        helper.setText(R.id.recycle_text, item.getName());
                    }
                }
            };

            binding.gvChooseSingleView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
            binding.gvChooseSingleView.setNumColumns(2);

        }

        if (gvSingleChooseAdapter != null) {

            binding.gvChooseSingleView.setAdapter(gvSingleChooseAdapter);

            gvSingleChooseAdapter.setData(mOriginList);

            // TODO: 2021/10/6 回显已选中逻辑
            for (int i = 0; i < mChooseList.size(); i++) {
                for (int j = 0; j < mOriginList.size(); j++) {
                    if (mChooseList.get(i).getCode() == mOriginList.get(j).getCode()) {
                        binding.gvChooseSingleView.setItemChecked(j, true);
                        break;
                    }
                }
            }
        }

    }

    private void initClick() {
        binding.dialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        binding.popFragmentSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChooseResultCallBack != null) {

                    SparseBooleanArray checkedItemPositions = binding.gvChooseSingleView.getCheckedItemPositions();
                    LogUtils.i("onClick: " + checkedItemPositions.toString());

                    List<Object> list = new ArrayList<>();
                    for (int i = 0; i < checkedItemPositions.size(); i++) {
                        int key = checkedItemPositions.keyAt(i);
                        boolean b = checkedItemPositions.get(key);
                        if (b) {
                            //key指的是该item在listview中的position
//                            System.out.println(key);
                            list.add(mOriginList.get(key));
                        }
                    }
                    mChooseResultCallBack.onResult(list);
                    dismiss();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}