package org.stepic.droid.view.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.squareup.otto.Subscribe;

import org.stepic.droid.R;
import org.stepic.droid.base.FragmentBase;
import org.stepic.droid.events.wifi_settings.WifiLoadIsChangedEvent;
import org.stepic.droid.view.custom.BetterSwitch;
import org.stepic.droid.view.dialogs.AllowMobileDataDialogFragment;
import org.stepic.droid.view.dialogs.VideoQualityDialog;

import butterknife.BindView;
import butterknife.BindString;
import butterknife.ButterKnife;

public class SettingsFragment extends FragmentBase {

    public static SettingsFragment newInstance() {
        return new SettingsFragment();
    }

    @BindView(R.id.fragment_settings_wifi_enable_switch)
    BetterSwitch mWifiLoadSwitch;

    @BindView(R.id.fragment_settings_external_player_switch)
    BetterSwitch mExternalPlayerSwitch;

    @BindView(R.id.video_quality_view)
    View mVideoQuality;

    @BindView(R.id.fragment_settings_notification_learn_switch)
    BetterSwitch notificationLearnSwitch;

    @BindView(R.id.fragment_settings_notification_vibration_switch)
    BetterSwitch notificationVibration;

    @BindView(R.id.fragment_settings_notification_sound_switch)
    BetterSwitch notificationSound;

    @BindView(R.id.storage_management_button)
    View storageManagementButton;

    @BindString(R.string.version)
    String versionPrefix;

    @BindString(R.string.clear_cache_title)
    String mClearCacheTitle;

    @BindString(R.string.kb)
    String kb;

    @BindString(R.string.mb)
    String mb;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_settings, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setUpNotificationVibration();

        setUpNotificationLearn();

        setUpSound();

        mWifiLoadSwitch.setChecked(!mSharedPreferenceHelper.isMobileInternetAlsoAllowed());//if first time it is true

        mExternalPlayerSwitch.setChecked(mUserPreferences.isOpenInExternal());

        mExternalPlayerSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mUserPreferences.setOpenInExternal(isChecked);
            }
        });


        mWifiLoadSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean newCheckedState) {
                if (mWifiLoadSwitch.isUserTriggered()) {
                    if (newCheckedState) {
                        //wifi only
                        bus.post(new WifiLoadIsChangedEvent(false));
                    } else {
                        //wifi and mobile internet
                        mWifiLoadSwitch.setChecked(true);
                        AllowMobileDataDialogFragment dialogFragment = new AllowMobileDataDialogFragment();
                        dialogFragment.show(getFragmentManager(), null);
                    }

                }
            }
        });


        final DialogFragment videoDialog = new VideoQualityDialog();
        mVideoQuality.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!videoDialog.isAdded()) {
                    videoDialog.show(getFragmentManager(), null);
                }
            }
        });

        storageManagementButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mShell.getScreenProvider().showStorageManagement(getActivity());
            }
        });

    }

    private void setUpNotificationVibration() {
        notificationVibration.setChecked(mUserPreferences.isVibrateNotificationEnabled());
        notificationVibration.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mUserPreferences.setVibrateNotificationEnabled(isChecked);
            }
        });
    }

    private void setUpSound() {
        notificationSound.setChecked(mUserPreferences.isSoundNotificationEnabled());
        notificationSound.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mUserPreferences.setNotificationSoundEnabled(isChecked);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        bus.register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        bus.unregister(this);
    }

    @Override
    public void onDestroyView() {
        mWifiLoadSwitch.setOnCheckedChangeListener(null);
        mExternalPlayerSwitch.setOnCheckedChangeListener(null);
        notificationLearnSwitch.setOnCheckedChangeListener(null);
        notificationVibration.setOnCheckedChangeListener(null);
        notificationSound.setOnCheckedChangeListener(null);
        storageManagementButton.setOnClickListener(null);
        super.onDestroyView();
    }


    @Subscribe
    public void onWifiChanged(WifiLoadIsChangedEvent e) {
        mWifiLoadSwitch.setChecked(!e.isNewStateMobileAllowed());
        storeMobileState(e.isNewStateMobileAllowed());
    }

    private void storeMobileState(boolean isMobileAllowed) {
        mSharedPreferenceHelper.setMobileInternetAndWifiAllowed(isMobileAllowed);
    }

    private void setUpNotificationLearn() {
        notificationLearnSwitch.setChecked(mUserPreferences.isNotificationEnabled());
        notificationLearnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mUserPreferences.setNotificationEnabled(isChecked);
            }
        });
    }
}

@@ -1454,16 +1454,117 @@
 Sound;%0A%0A
+    @BindView(R.id.fragment_settings_calendar_widget_switch)%0A    BetterSwitch calendarWidgetSwitch;%0A%0A
     @Bin
@@ -2888,32 +2888,439 @@
  %7D%0A        %7D);%0A%0A
+        calendarWidgetSwitch.setChecked(mUserPreferences.isNeedToShowCalendarWidget());%0A%0A        calendarWidgetSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() %7B%0A            @Override%0A            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) %7B%0A                mUserPreferences.setNeedToShowCalendarWidget(isChecked);%0A            %7D%0A        %7D);%0A%0A
 %0A        mWifiLo
@@ -5879,24 +5879,87 @@
 royView() %7B%0A
+        calendarWidgetSwitch.setOnCheckedChangeListener(null);%0A
         mWif
@@ -6354,17 +6354,16 @@
 %0A    %7D%0A%0A
-%0A
     @Sub

add calendar widget to controller of settings