package com.lemenzo.clock.settings;

import android.app.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.lemenzo.clock.R;
import com.lemenzo.clock.util.Ui;

public final class SettingsActivity extends Activity {
    private ClockSettings s;
    private LinearLayout root;

    @Override protected void onCreate(Bundle b){
        super.onCreate(b);
        s=new ClockSettings(this);
        build();
    }

    private void build(){
        ScrollView scroll=new ScrollView(this);
        scroll.setFillViewport(true);
        scroll.setBackgroundColor(Ui.BLACK);
        root=new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setPadding(Ui.dp(this,20),Ui.dp(this,14),Ui.dp(this,20),Ui.dp(this,40));
        root.setBackgroundColor(Ui.BLACK);
        scroll.addView(root,new ScrollView.LayoutParams(-1,-2));

        root.addView(Ui.subpageTopBar(this,getString(R.string.settings_title)));
        TextView subtitle=Ui.text(this,getString(R.string.settings_subtitle),14,Ui.SILVER);
        subtitle.setPadding(0,0,0,Ui.dp(this,6));root.addView(subtitle);
        TextView privacyBadge=Ui.eyebrow(this,getString(R.string.privacy_badge));
        root.addView(privacyBadge,new LinearLayout.LayoutParams(-2,Ui.dp(this,28)));
        root.addView(Ui.space(this,14));

        root.addView(Ui.sectionLabel(this,getString(R.string.alarm_settings)));
        LinearLayout alarm=Ui.premiumCard(this);
        addChoice(alarm,getString(R.string.default_snooze),minutesLabel(s.defaultSnooze()),()->showSnoozeChoice());
        addGap(alarm);
        Switch gradual=Ui.toggle(this,getString(R.string.gradual_volume_default),s.gradualDefault());
        gradual.setOnCheckedChangeListener((v,on)->s.setGradualDefault(on));alarm.addView(gradual);
        addGap(alarm);
        Switch vibrate=Ui.toggle(this,getString(R.string.alarm_vibration_default),s.alarmVibrateDefault());
        vibrate.setOnCheckedChangeListener((v,on)->s.setAlarmVibrateDefault(on));alarm.addView(vibrate);
        root.addView(alarm);

        root.addView(Ui.sectionLabel(this,getString(R.string.behavior_settings)));
        LinearLayout behavior=Ui.premiumCard(this);
        addChoice(behavior,getString(R.string.volume_buttons),volumeLabel(s.volumeButtons()),()->showVolumeChoice());
        addGap(behavior);
        addChoice(behavior,getString(R.string.silence_after),silenceLabel(s.silenceAfterMinutes()),()->showSilenceChoice());
        addGap(behavior);
        Switch timerVibrate=Ui.toggle(this,getString(R.string.timer_vibration),s.timerVibrate());
        timerVibrate.setOnCheckedChangeListener((v,on)->s.setTimerVibrate(on));behavior.addView(timerVibrate);
        root.addView(behavior);

        root.addView(Ui.sectionLabel(this,getString(R.string.privacy_title)));
        LinearLayout privacy=Ui.premiumCard(this);
        TextView detail=Ui.text(this,getString(R.string.privacy_detail),15,Ui.SILVER);
        detail.setLineSpacing(0,1.16f);privacy.addView(detail);root.addView(privacy);

        root.addView(Ui.sectionLabel(this,getString(R.string.about_lemenzo_clock)));
        LinearLayout about=Ui.premiumCard(this);
        about.addView(Ui.title(this,getString(R.string.app_titel),20));
        TextView aboutSummary=Ui.text(this,getString(R.string.about_clock_summary),14,Ui.SILVER);aboutSummary.setPadding(0,Ui.dp(this,5),0,Ui.dp(this,12));about.addView(aboutSummary);
        about.addView(Ui.settingsRow(this,getString(R.string.version_label),getString(R.string.version_1_0)));
        about.addView(Ui.space(this,8));
        about.addView(Ui.settingsRow(this,getString(R.string.platform_label),getString(R.string.lemenzoos_name)));
        root.addView(about);

        setContentView(scroll);
        Ui.applySystemSafeInsets(this,scroll);
    }

    private void addChoice(LinearLayout parent,String title,String value,Runnable action){
        LinearLayout row=Ui.settingsRow(this,title,value);
        row.setOnClickListener(v->action.run());parent.addView(row,new LinearLayout.LayoutParams(-1,-2));
    }
    private void addGap(LinearLayout parent){parent.addView(Ui.space(this,8));}

    private void showSnoozeChoice(){
        final int[] values={5,10,15,20,30};
        String[] labels=new String[values.length];int checked=0;
        for(int i=0;i<values.length;i++){labels[i]=minutesLabel(values[i]);if(values[i]==s.defaultSnooze())checked=i;}
        showSingleChoice(getString(R.string.default_snooze),labels,checked,which->{s.setDefaultSnooze(values[which]);rebuild();});
    }
    private void showVolumeChoice(){
        final String[] values={ClockSettings.VOLUME_SNOOZE,ClockSettings.VOLUME_DISMISS,ClockSettings.VOLUME_VOLUME};
        String[] labels={getString(R.string.snooze),getString(R.string.dismiss),getString(R.string.change_volume)};int checked=0;
        for(int i=0;i<values.length;i++)if(values[i].equals(s.volumeButtons()))checked=i;
        showSingleChoice(getString(R.string.volume_buttons),labels,checked,which->{s.setVolumeButtons(values[which]);rebuild();});
    }
    private void showSilenceChoice(){
        final int[] values={0,5,10,15,30};
        String[] labels={getString(R.string.never),minutesLabel(5),minutesLabel(10),minutesLabel(15),minutesLabel(30)};int checked=0;
        for(int i=0;i<values.length;i++)if(values[i]==s.silenceAfterMinutes())checked=i;
        showSingleChoice(getString(R.string.silence_after),labels,checked,which->{s.setSilenceAfterMinutes(values[which]);rebuild();});
    }

    private interface Choice{void selected(int which);}
    private void showSingleChoice(String title,String[] labels,int checked,Choice choice){
        AlertDialog d=new AlertDialog.Builder(this).setTitle(title).setSingleChoiceItems(labels,checked,(dialog,which)->{
            choice.selected(which);dialog.dismiss();
        }).setNegativeButton(R.string.cancel,null).create();
        Ui.showStyled(d);
    }

    private String minutesLabel(int minutes){return getString(R.string.timer_quick_label,minutes);}
    private String silenceLabel(int minutes){return minutes<=0?getString(R.string.never):minutesLabel(minutes);}
    private String volumeLabel(String value){
        if(ClockSettings.VOLUME_DISMISS.equals(value))return getString(R.string.dismiss);
        if(ClockSettings.VOLUME_VOLUME.equals(value))return getString(R.string.change_volume);
        return getString(R.string.snooze);
    }
    private void rebuild(){root=null;build();}
}
