package com.lemenzo.multisms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public final class MainActivity extends Activity {
    GroupStore store;
    LinearLayout list;

    public void onCreate(Bundle b) {
        super.onCreate(b);
        store = new GroupStore(this);
        render();
    }

    protected void onResume() {
        super.onResume();
        if (list != null) fill();
    }

    void render() {
        ScrollView sv = new ScrollView(this);
        sv.setFillViewport(true);
        LinearLayout r = Ui.root(this);
        sv.addView(r);

        r.addView(Ui.brandHeader(this));
        Ui.gap(this, r, 22);

        LinearLayout heading = new LinearLayout(this);
        heading.setGravity(Gravity.CENTER_VERTICAL);

        TextView groups = Ui.text(this, getString(R.string.groups), 24, true);
        heading.addView(groups, new LinearLayout.LayoutParams(0, -2, 1f));

        Button add = Ui.button(this, getString(R.string.new_group_short));
        add.setMinHeight(Ui.dp(this, 44));
        add.setOnClickListener(v ->
                startActivity(new Intent(this, EditGroupActivity.class)));
        heading.addView(add, new LinearLayout.LayoutParams(-2, Ui.dp(this, 44)));
        r.addView(heading);

        Ui.gap(this, r, 7);
        TextView intro = Ui.text(this, getString(R.string.groups_description), 13, false);
        intro.setTextColor(getColor(R.color.lemenzo_text_secondary));
        intro.setLineSpacing(0f, 1.08f);
        r.addView(intro);

        Ui.gap(this, r, 18);
        list = new LinearLayout(this);
        list.setOrientation(LinearLayout.VERTICAL);
        r.addView(list);

        setContentView(sv);
        fill();
    }

    void fill() {
        list.removeAllViews();
        java.util.List<GroupStore.Group> groups = store.groups();

        if (groups.isEmpty()) {
            LinearLayout empty = Ui.softCard(this);
            TextView title = Ui.text(this, getString(R.string.no_groups), 17, true);
            empty.addView(title);
            Ui.gap(this, empty, 6);
            TextView hint = Ui.text(this, getString(R.string.no_groups_description), 13, false);
            hint.setTextColor(getColor(R.color.lemenzo_text_secondary));
            empty.addView(hint);
            list.addView(empty);
            return;
        }

        for (GroupStore.Group g : groups) {
            LinearLayout card = Ui.softCard(this);

            LinearLayout top = new LinearLayout(this);
            top.setGravity(Gravity.CENTER_VERTICAL);
            TextView name = Ui.text(this, g.name, 18, true);
            top.addView(name, new LinearLayout.LayoutParams(0, -2, 1f));

            TextView arrow = Ui.text(this, "›", 28, false);
            arrow.setTextColor(getColor(R.color.lemenzo_blue));
            top.addView(arrow, new LinearLayout.LayoutParams(-2, -2));
            card.addView(top);

            Ui.gap(this, card, 4);
            TextView count = Ui.text(this, getString(R.string.recipients, g.count), 13, false);
            count.setTextColor(getColor(R.color.lemenzo_text_secondary));
            card.addView(count);

            card.setOnClickListener(v ->
                    startActivity(new Intent(this, ComposeActivity.class)
                            .putExtra("group_id", g.id)));
            card.setOnLongClickListener(v -> {
                startActivity(new Intent(this, EditGroupActivity.class)
                        .putExtra("group_id", g.id));
                return true;
            });

            list.addView(card);
            Ui.gap(this, list, 10);
        }
    }
}
