package com.xixun.launcher;

import androidx.fragment.app.FragmentActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;

public class ProgActivity extends FragmentActivity {

    GridLayout _grid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prog);

        _grid = findViewById(R.id.grid_layout);
        Button btnNew = findViewById(R.id.BtnNew);
        btnNew.setOnClickListener((View v)->startActivity(new Intent(ProgActivity.this, ProgEditActi.class)));

        var programsDir = new File(Util.programsDir);
        programsDir.mkdirs();
        var progs = programsDir.listFiles();
        File cover;
        for(var prog : progs) if(prog.isDirectory() && (cover = new File(prog, "cover.png")).isFile()) add(prog.getName(), cover);
        for(int i=0; i<5; i++) {
            var vv = new LinearLayout(this);
            vv.setOrientation(LinearLayout.VERTICAL);
            var imgV = new ImageView(this);
            imgV.setImageResource(R.mipmap.ic_launcher);
            imgV.setFocusable(true);
            var prm = new LinearLayout.LayoutParams(480, 270);
            vv.addView(imgV, prm);
            var text = new TextView(this);
            text.setText("Prog "+i);
            vv.addView(text);

            var hhh = new LinearLayout(this);
            var btn = new Button(this);
            btn.setText("编辑");
            hhh.addView(btn);

            btn = new Button(this);
            btn.setText("删除");
            hhh.addView(btn);

            vv.addView(hhh);

            _grid.addView(vv);
        }
    }

    void add(String name, File cover) {
        var item = new LinearLayout(this);
        item.setOrientation(LinearLayout.VERTICAL);
        var imgV = new ImageView(this);
        imgV.setImageURI(Uri.fromFile(cover));
        imgV.setFocusable(true);
        item.addView(imgV, new LinearLayout.LayoutParams(480, 270));
        var text = new TextView(this);
        text.setText(name);
        item.addView(text);

        var hhh = new LinearLayout(this);
        var btn = new Button(this);
        btn.setText("编辑");
        hhh.addView(btn);

        btn = new Button(this);
        btn.setText("删除");
        hhh.addView(btn);

        item.addView(hhh);
        _grid.addView(item);
    }
}