package com.qhclh.ytzh.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.utils.DateUtils;
import com.qhclh.ytzh.utils.widget.DateTimePickDialog;
import com.qhclh.ytzh.work.Hatchery.ChoseTimeEvent;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.qhclh.ytzh.utils.DateUtils.PATTERN_TRANSVERSE;

/**
 * Created by 青花瓷 on 2018/4/3.
 */

public class Chose2timePopuWindow extends PopupWindow {
    private View conentView;
    private Activity context;
    public Chose2timePopuWindow(final Activity context){
        super(context);
        this.context = context;
        this.initPopupWindow();
    }

    private Date startDate = null;
    private Date endDate = null;
    private void initPopupWindow() {
        //使用view来引入布局
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.pop_chosetime, null);
        //获取popupwindow的高度与宽度
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);

        LinearLayout ll_pop_start = conentView.findViewById(R.id.ll_pop_start);
        LinearLayout ll_pop_end = conentView.findViewById(R.id.ll_pop_end);
        TextView pop_btn = conentView.findViewById(R.id.pop_btn);

        final TextView tv_start = conentView.findViewById(R.id.tv_start);
        final TextView tv_end = conentView.findViewById(R.id.tv_end);

        ll_pop_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DateTimePickDialog mDateTimePickDialog = new DateTimePickDialog(context, new DateTimePickDialog.DateChangedCallBack() {
                    @Override
                    public void onDateChanged(Date date) {
                        String start = DateUtils.format(date, PATTERN_TRANSVERSE);
                        tv_start.setText(start);
                        try {
                            startDate = new SimpleDateFormat("yyyy-MM-dd").parse(start);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                });
                mDateTimePickDialog.show();
            }
        });

        ll_pop_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DateTimePickDialog mDateTimePickDialog = new DateTimePickDialog(context, new DateTimePickDialog.DateChangedCallBack() {
                    @Override
                    public void onDateChanged(Date date) {
                        String end = DateUtils.format(date, PATTERN_TRANSVERSE);
                        tv_end.setText(end);
                        try {
                            endDate = new SimpleDateFormat("yyyy-MM-dd").parse(end);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                });
                mDateTimePickDialog.show();
            }
        });

        pop_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (startDate == null){
                    Toast.makeText(context,"请选择起始时间",Toast.LENGTH_SHORT).show();
                    return;
                }else if (endDate == null){
                    Toast.makeText(context,"请选择终止时间",Toast.LENGTH_SHORT).show();
                    return;
                }
                EventBus.getDefault().post(new ChoseTimeEvent(startDate,endDate));
                dismissPopupWindow();
            }
        });

    }



    public void showPopupWindow() {
        if (!this.isShowing()) {
            this.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        } else {
            this.dismiss();
        }
    }

    private void dismissPopupWindow(){
        if (isShowing()){
            this.dismiss();
        }
    }

}
