package com.example.nhocg.myseftcalendar.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;

import com.example.nhocg.myseftcalendar.R;
import com.example.nhocg.myseftcalendar.helper.Constant;
import com.example.nhocg.myseftcalendar.helper.DBHelper;
import com.example.nhocg.myseftcalendar.helper.DBStringHelper;
import com.example.nhocg.myseftcalendar.helper.FunctionHelper;
import com.example.nhocg.myseftcalendar.datas.entities.DayLesson;
import com.rengwuxian.materialedittext.MaterialEditText;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static com.example.nhocg.myseftcalendar.dialog.AddEditDetailCancelDialogFragment.AddEditDetailCancelDialogMode.CANCEL_ADD_MODE;
import static com.example.nhocg.myseftcalendar.dialog.AddEditDetailCancelDialogFragment.AddEditDetailCancelDialogMode.CANCEL_EDIT_MODE;
import static com.example.nhocg.myseftcalendar.dialog.AddEditWeekLessonDialogFragment.DAY_LESSON_DIALOG_ITEM;
import static com.example.nhocg.myseftcalendar.dialog.AddEditWeekLessonDialogFragment.DAY_LESSON_DIALOG_ITEM_POS;

/**
 * Created by nhocg on 12/2/2017.
 */

public class AddEditDetailCancelDialogFragment extends DialogFragment {

    private static final String TAG = AddEditDetailCancelDialogFragment.class.getSimpleName();

    //data
    private FragmentActivity mActivity;
    private Resources mResources;
    private DBHelper mDbHelper;
    private DBStringHelper mDbStringHelper;
    private DayLesson mDayLesson = null;
    private int pos = Constant.INT_NONE_VALUE;

    private MaterialEditText mView_CancelReason;
    private @AddEditDetailCancelDialogMode
    int Mode = Constant.INT_NONE_VALUE;
    private AddEditDetailCancelDialogFragmentListener mListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (AddEditDetailCancelDialogFragmentListener) getActivity();
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(getActivity().toString()
                    + " must implement AddEditDetailCancelDialogFragmentListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        if (!initData()) {
            this.getDialog().cancel();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        // Get the layout inflater
        LayoutInflater inflater = mActivity.getLayoutInflater();

        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_fragment_add_edit_detail_cancel, null);

        initView(view);

        builder.setView(view)
                .setTitle(getTitle());

        setButton(builder);

        return builder.create();
    }

    private boolean initData() {
        mActivity = getActivity();
        mResources = mActivity.getResources();
        mDbHelper = new DBHelper(mActivity);
        mDbStringHelper = new DBStringHelper(mActivity);

        Bundle bundle = getArguments();
        mDayLesson = bundle.getParcelable(DAY_LESSON_DIALOG_ITEM);
        pos = bundle.getInt(DAY_LESSON_DIALOG_ITEM_POS);
        if (mDayLesson == null) {
            return false;
        } else {
            Mode = mDayLesson.isDay_lesson_is_cancel() ? CANCEL_EDIT_MODE : CANCEL_ADD_MODE;
            return true;
        }
    }

    private void initView(View view) {
        mView_CancelReason = view.findViewById(R.id.cancel_reason);
        setEditValue();
    }

    private void setEditValue() {
        if (Mode == CANCEL_EDIT_MODE) {
            mView_CancelReason.setText(mDayLesson.getDay_lesson_status_reason_change());
        }
    }

    private String getTitle() {
        switch (Mode) {
            case AddEditDetailCancelDialogMode.CANCEL_ADD_MODE:
                return mResources.getString(R.string.cancel_day_lesson);
            case AddEditDetailCancelDialogMode.CANCEL_EDIT_MODE:
                return mResources.getString(R.string.edit_cancel_day_lesson);
        }
        return "";
    }

    private void setButton(AlertDialog.Builder builder) {

        switch (Mode) {
            case AddEditDetailCancelDialogMode.CANCEL_ADD_MODE:
                builder
                        // Add action buttons
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                doCancel(true);
                            }
                        })
                        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                AddEditDetailCancelDialogFragment.this.getDialog().cancel();
                            }
                        });
                break;


            case AddEditDetailCancelDialogMode.CANCEL_EDIT_MODE:
                builder
                        // Add action buttons
                        .setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                doCancel(true);
                            }
                        })
                        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                AddEditDetailCancelDialogFragment.this.getDialog().cancel();
                            }
                        })
                        .setNeutralButton(R.string.remove, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doCancel(false);
                            }
                        });
                break;
        }
    }

    private void doCancel(boolean isCancel) {
        if (mDayLesson == null) return;

        mDayLesson.setDay_lesson_is_cancel(isCancel);
        if (isCancel) {
            mDayLesson.setDay_lesson_status_reason_change(mView_CancelReason.getText().toString());
        } else {
            mDayLesson.setDay_lesson_status_reason_change("");
        }

        DBHelper.DATABASE_CODE database_code = mDbHelper.updateDayLessonCancel(mDayLesson);
        if (database_code != DBHelper.DATABASE_CODE.UPDATE_DAY_LESSON_SUCCESS) {
            FunctionHelper.makeLToast(mActivity, mDbStringHelper.getDBString(database_code));
            this.getDialog().cancel();
            return;
        }
        mListener.onDoCancel(mDayLesson, pos);
    }


    @Retention(RetentionPolicy.SOURCE)
    @IntDef({CANCEL_ADD_MODE, CANCEL_EDIT_MODE})
    public @interface AddEditDetailCancelDialogMode {
        int CANCEL_ADD_MODE = 3 << 1;
        int CANCEL_EDIT_MODE = 3 << 2;
    }

    public interface AddEditDetailCancelDialogFragmentListener {
        void onDoCancel(DayLesson dayLesson, int pos);
    }
}