package com.tofly.ghrq_route.common.ui.dialog;

import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.mgq.base.utils.progressdialog.ProgressManager;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.module.plan.controller.PlanCallBack;
import com.tofly.ghrq_route.module.plan.controller.PlanController;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.Date;
import java.util.GregorianCalendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/12/27.
 */

public class PlanApplyRecalculateDialog extends BottomSheetDialog {
    @BindView(R.id.bottom_sheet_detail_header)
    RelativeLayout bottomSheetDetailHeader;
    @BindView(R.id.layout_date_picker)
    LinearLayout layoutPickDate;
    @BindView(R.id.text_date_selected)
    TextView textDateSelected;
    @BindView(R.id.btn_apply_recalculate)
    Button btnApplyRecal;

    private final static String TAG = PlanApplyRecalculateDialog.class.getName();

    private Context mContext;

    private PlanBean mPlan;

    private DatePickerDialog datePickerDialog;

    private String mDatePicked;

    private PlanController planController;

    public PlanApplyRecalculateDialog(@NonNull Context context, @NonNull PlanBean plan) {
        super(context);
        mContext = context;
        mPlan = plan;
    }

    private OnApplyRecalculate onApplyRecalculate;
    public void setOnApplyRecalculate(OnApplyRecalculate applyRecalculate){
        onApplyRecalculate = applyRecalculate;
    }

    public interface OnApplyRecalculate{
        void onApplyResult(boolean isSuccess);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_plan_recalculate_bottom);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);

        initView();
        initData();
        initListener();
    }

    private void initView() {

    }

    private void initData() {
        String today = DateUtils.getYMD();
        mDatePicked = today;
        textDateSelected.setText(today);
    }

    private void initListener(){

    }

    @OnClick({R.id.layout_date_picker, R.id.btn_apply_recalculate, R.id.btn_cancel})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.layout_date_picker:{
                showDatePickerDialog();
            }
            break;
            case R.id.btn_apply_recalculate:{
                applyRecalculatePlan(mDatePicked);
            }
            break;
            case R.id.btn_cancel:{
                dismiss();
            }
            break;
        }
    }

    private void applyRecalculatePlan(String date){
        UserBean userBean = BaseApp.getInstance().getLoginUser();
        if (userBean == null)
            return;

        if (mPlan == null)
            return;

        if (planController == null)
            planController = new PlanController(mContext);
        String strUploading = mContext.getResources().getString(R.string.uploading_data);
        ProgressManager.getInstance().showProgress(mContext, null, strUploading, false);
        planController.applyRecalculatePlanPercent(userBean.getToken(), mPlan.getPlan_id(), date, new PlanCallBack(){
            @Override
            public void onApplyRecalculatePlanPercent(boolean isSuccess, int errorCode, String errMsg) {
                super.onApplyRecalculatePlanPercent(isSuccess, errorCode, errMsg);
                ProgressManager.getInstance().cancelProgress();
                if (isSuccess){
                    ToastUtil.showToast(mContext, errMsg);
                    dismiss();
                    if (onApplyRecalculate != null){
                        onApplyRecalculate.onApplyResult(isSuccess);
                    }
                }else {
                    ToastUtil.showToast(mContext, errMsg);
                }
            }
        });
    }

    private void showDatePickerDialog() {
        String date = textDateSelected.getText().toString();
        String strBeignDate = mPlan.getPlan_begindate();
        if (date != null && !date.isEmpty()) {
            int[] dateArray = DateUtils.getYMDArray(date, "-");
            int year = dateArray[0];
            int month = dateArray[1];
            int day = dateArray[2];
            datePickerDialog = new DatePickerDialog(mContext, android.R.style.Widget_Material_DatePicker, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    GregorianCalendar dateSet = new GregorianCalendar(year, monthOfYear, dayOfMonth);
                    String ymd = DateUtils.getYMD(dateSet.getTime().getTime());
                    if (ymd != null && !ymd.isEmpty() && !mDatePicked.equals(ymd)) {
                        mDatePicked = ymd;
                        textDateSelected.setText(ymd);
                    }
                }
            }, year, month, day);
            DatePicker datePicker = datePickerDialog.getDatePicker();
            long dateBegin = DateUtils.getStrTimeLong(strBeignDate);
            datePicker.setMinDate(dateBegin);
            datePicker.setMaxDate(new Date().getTime());
            datePickerDialog.show();
        }
    }
}
