package com.zztrapp.zztrapp.ui.currency.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import com.github.gzuliyujiang.wheelpicker.DatimePicker;
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode;
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode;
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimePickedListener;
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity;
import com.github.gzuliyujiang.wheelpicker.widget.DatimeWheelLayout;
import com.zztrapp.zztrapp.base.BaseActivity;
import com.zztrapp.zztrapp.bean.HttpResult;
import com.zztrapp.zztrapp.bean.MeetingBean;
import com.zztrapp.zztrapp.databinding.ActivityAddAppointmentMeetingBinding;
import com.zztrapp.zztrapp.http.HttpMethods;
import com.zztrapp.zztrapp.http.ISubscriberOnNextListener;
import com.zztrapp.zztrapp.http.ProgressSubscriber;
import com.zztrapp.zztrapp.http.RxApiManager;
import com.zztrapp.zztrapp.utilcode.util.TimeUtils;
import com.zztrapp.zztrapp.utilcode.util.ToastUtils;

import java.util.HashMap;

/**
 * Describe 添加预约会议
 * Created by Bby on 2022/6/29
 * Email bby0856@163.com
 */
public class AddAppointmentMeetingActivity extends BaseActivity {


    private ActivityAddAppointmentMeetingBinding binding;

    @Override
    public View getLayoutId() {
        return binding.getRoot();
    }

    @Override
    protected void initBinding() {
        binding = ActivityAddAppointmentMeetingBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void init() {
        binding.navView.navTitle.setText("添加预约会议");
        binding.navView.navBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initOnClick() {
        binding.kssjView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatimePicker picker = new DatimePicker(AddAppointmentMeetingActivity.this);
                final DatimeWheelLayout wheelLayout = picker.getWheelLayout();
                picker.setOnDatimePickedListener(new OnDatimePickedListener() {
                    @Override
                    public void onDatimePicked(int year, int month, int day, int hour, int minute, int second) {
                        String text = year + "-" + numFormat(month) + "-" + numFormat(day) + " " + numFormat(hour) + ":" + numFormat(minute);
//                        text += wheelLayout.getTimeWheelLayout().isAnteMeridiem() ? " 上午" : " 下午";
//                        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
                        binding.kssj.setText(text);
                    }
                });
                wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
                wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND);
                wheelLayout.setRange(DatimeEntity.now(), DatimeEntity.yearOnFuture(10));
                wheelLayout.setDateLabel("-", "-", "");
                wheelLayout.setTimeLabel("：", "", "");
                picker.show();
            }
        });
        binding.jssjView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatimePicker picker = new DatimePicker(AddAppointmentMeetingActivity.this);
                final DatimeWheelLayout wheelLayout = picker.getWheelLayout();
                picker.setOnDatimePickedListener(new OnDatimePickedListener() {
                    @Override
                    public void onDatimePicked(int year, int month, int day, int hour, int minute, int second) {
                        String text = year + "-" + numFormat(month) + "-" + numFormat(day) + " " + numFormat(hour) + ":" + numFormat(minute);
//                        text += wheelLayout.getTimeWheelLayout().isAnteMeridiem() ? " 上午" : " 下午";
//                        Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
                        binding.jssj.setText(text);
                    }
                });
                wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY);
                wheelLayout.setTimeMode(TimeMode.HOUR_24_NO_SECOND);
                wheelLayout.setRange(DatimeEntity.now(), DatimeEntity.yearOnFuture(10));
                wheelLayout.setDateLabel("-", "-", "");
                wheelLayout.setTimeLabel("：", "", "");
                picker.show();
            }
        });

        binding.openMeeting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String hyzt = binding.hyzt.getText().toString();
                String kssj = binding.kssj.getText().toString();
                String jssj = binding.jssj.getText().toString();
                if (TextUtils.isEmpty(hyzt)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("请输入会议主题");
                } else if (TextUtils.isEmpty(kssj)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("请选择开始时间");
                } else if (TextUtils.isEmpty(jssj)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("请选择结束时间");
                } else {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("hymc", hyzt);
                    map.put("kssj", TimeUtils.string2Millis(kssj + ":00"));
                    map.put("jssj", TimeUtils.string2Millis(jssj + ":00"));
                    map.put("bz", binding.bz.getText().toString());
                    createSphy(map);
                }
            }
        });

    }

    private String numFormat(int num) {
        String s = "";
        if (num < 10) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    //提交
    private void createSphy(HashMap<String, Object> map) {
        ISubscriberOnNextListener<HttpResult<MeetingBean>> iSubscriberOnNextListener = new ISubscriberOnNextListener<HttpResult<MeetingBean>>() {
            @Override
            public void onNext(HttpResult<MeetingBean> httpResult) {
                if (httpResult.getCode() == 200) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show("预约成功");
                    finish();
                } else {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(httpResult.getMessage());
                }
            }

            @Override
            public void onError(int code, String message) {
                ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).show(message);
            }
        };
        ProgressSubscriber<HttpResult<MeetingBean>> httpResultProgressSubscriber = new ProgressSubscriber<>(iSubscriberOnNextListener, this, true);
        RxApiManager.get().add("createSphy", httpResultProgressSubscriber);
        HttpMethods.getInstance(this).createSphy(httpResultProgressSubscriber, map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
        RxApiManager.get().cancel("createSphy");
    }
}