package com.monthyearpicker.builder;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.view.LayoutInflater;
import android.view.View;

import androidx.fragment.app.FragmentActivity;

import com.monthyearpicker.R;
import com.monthyearpicker.RNMonthPickerDialog;
import com.monthyearpicker.adapter.RNMonthPickerProps;

import java.util.Objects;

public class PickerViewFactory {

  private static final int DARK_VIEW = R.layout.picker_list_view_dark;
  private static final int LIGHT_VIEW = R.layout.picker_list_view;
  private static final int DARK_STYLE = R.style.DarkStyle ;
  private static final int LIGHT_STYLE = R.style.LightStyle;

  private RNMonthPickerProps props;
  private RNMonthPickerDialog rnMonthPickerDialog;

  public PickerViewFactory(RNMonthPickerProps props, RNMonthPickerDialog rnMonthPickerDialog) {
    this.props = props;
    this.rnMonthPickerDialog = rnMonthPickerDialog;
  }

  /**
   * get dialog style base on autoTheme and uiMode
   */
  public int getDialogStyle(Boolean autoTheme  , int uiMode) {
    if (uiMode == Configuration.UI_MODE_NIGHT_YES && autoTheme) {
      return DARK_STYLE;
    } else {
      if (Objects.equals(props.themeVariant(), "dark")) {
        return DARK_STYLE;
      } else {
        return LIGHT_STYLE;

      }
    }
  }

  /**
   * get content view style base on autoTheme and uiMode
   */
  public int getContentViewStyle(Boolean autoTheme , int uiMode) {
    if (uiMode == Configuration.UI_MODE_NIGHT_YES && autoTheme) {
      return DARK_VIEW;
    } else {
      if (Objects.equals(props.themeVariant(), "dark")) {
        return DARK_VIEW;
      } else {
        return LIGHT_VIEW;
      }
    }
  }

  public AlertDialog build() {
    if (rnMonthPickerDialog.getActivity() == null) {
      throw new NullPointerException();
    }
    final FragmentActivity fragmentActivity = rnMonthPickerDialog.getActivity();
    final int uiMode = fragmentActivity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;

    // dialog style
    final int dialogStyle = getDialogStyle(props.autoTheme(), uiMode);
    final int contentViewStyle = getContentViewStyle(props.autoTheme(), uiMode);

    AlertDialog.Builder builder = new AlertDialog.Builder(rnMonthPickerDialog.getActivity(), dialogStyle);
    LayoutInflater inflater = fragmentActivity.getLayoutInflater();
    View contentView = inflater.inflate(contentViewStyle, null);

    MonthYearScrollListener scrollListener = new MonthYearScrollListener();
    final MonthYearNumberPicker monthPicker = new MonthNumberPicker()
        .view(contentView)
        .props(props)
        .onScrollListener(scrollListener)
        .build();
    final MonthYearNumberPicker yearPicker = new YearNumberPicker()
        .view(contentView)
        .props(props)
        .onScrollListener(scrollListener)
        .build();

    scrollListener.addObserver(monthPicker);
    scrollListener.addObserver(yearPicker);

    builder.setView(contentView)
        .setPositiveButton(props.okButton(), new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            props.onChange(yearPicker.getValue(), monthPicker.getValue(), 0);
            rnMonthPickerDialog.getDialog().cancel();
          }
        })
        .setNegativeButton(props.cancelButton(), new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int id) {
            rnMonthPickerDialog.getDialog().cancel();
          }
        });

    return builder.create();
  }
}
