package com.android.chmo.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.chmo.R;
import com.android.chmo.utils.PixelUtils;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;

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

/**
 * Created by Administrator on 2018/8/16.
 */

public class PickNumDialog extends Dialog {

    private TextView titleView;
    private WheelView wheelView;
    private PickCallback pickCallback;
    private String title;

    private List<Integer> numList = new ArrayList<>();


    public PickNumDialog(Context context) {
        super(context, R.style.DialogStyle);
    }

    public PickNumDialog setPickCallback(PickCallback callback) {
        pickCallback = callback;
        return this;
    }

    public PickNumDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public PickNumDialog setNum(int start, int end) {
        numList.clear();
        for (int i=start; i <= end;i++) {
            numList.add(i);
        }
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pick_num);
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = PixelUtils.getWindowWidth();
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);

        titleView = (TextView) findViewById(R.id.title);
        wheelView = (WheelView) findViewById(R.id.wheel);

        if(!TextUtils.isEmpty(title)) {
            titleView.setText(title);
        }

        wheelView.setAdapter(new NumAdapter());
        wheelView.setCurrentItem(0);
        wheelView.setCyclic(false);

        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ok();
                dismiss();
            }
        });
    }

    private void ok() {
        if (null != pickCallback) {
            pickCallback.onPick(numList.get(wheelView.getCurrentItem()));
        }
    }


    private class NumAdapter implements WheelAdapter<String>{

        @Override
        public int getItemsCount() {
            return numList.size();
        }

        @Override
        public String getItem(int index) {
            if (index >= 0 && index < numList.size()) {
                return numList.get(index) + "小时";
            }
            return "";
        }

        @Override
        public int indexOf(String o) {
           String num = o.replace("小时","");
           return numList.indexOf(Integer.parseInt(num));
        }
    }



    public interface PickCallback {
        void onPick(int num);
    }

}
