package com.example.fuckyou;

import static com.example.fuckyou.MainActivity.kebiaodata;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class showkebiao extends AppCompatActivity {
    private static List<TextView> alltext=new ArrayList<>();
    public static Date firstSunday(Date startdate)
    {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(startdate);
        for (int i=0;i>=-6;i--)
        {
            calendar.add(Calendar.DATE,i);
            if (calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
            {
                break;
            }
        }
        return calendar.getTime();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_showkebiao);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        for (int i=23;i<=113;i++) {
            int resID = getResources().getIdentifier("textView" + i, "id", getPackageName());
            TextView textView = findViewById(resID);
            if (textView != null) {
                alltext.add(textView);
                textView.setText("");
            }
        }
        MyDatabaseHelper db=new MyDatabaseHelper(this);
        List<Lesson> kebiaodata= null;
        /*try {
            kebiaodata = db.alllesson();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }*/

        Date date=new Date();
        Log.d("timing",date.toString());
        Date thissunday=firstSunday(date);
        try {
            kebiaodata=db.lessoninweek(thissunday);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        for (Lesson test:kebiaodata)
        {
            int num=13*test.day+test.fromClass-1;
            TextView temp= alltext.get(num);
            int cutoff = 4;
            String firstPart = test.name.substring(0, Math.min(cutoff, test.name.length()));
            String secondPart = test.name.length() > cutoff ? test.name.substring(cutoff) : "";
            temp.setText(firstPart);
            temp.setBackgroundColor(Color.parseColor("#FFBB33"));
            for(int i=1;i<=test.endClass-test.fromClass;i++)
            {
                TextView tt=alltext.get(num+i);
                tt.setText(secondPart);
                tt.setBackgroundColor(Color.parseColor("#FFBB33"));
                if (i>=3)
                {
                    tt.setText("same");
                }
            }
        }
    }
}