package com.example.tampopo_client.views;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.tampopo_client.R;
import com.example.tampopo_client.Tampopo;
import com.example.tampopo_client.views.MyFragmentAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import androidx.lifecycle.ViewModelProvider;
import com.example.tampopo_client.viewmodels.FriendSentRequestViewModel;

public class FriendActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private MyFragmentAdapter adapter;
    // フィールド追加
    private FriendSentRequestViewModel friendSentRequestViewModel;
    Tampopo tampopo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        tampopo = (Tampopo)getApplication();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend);

        viewPager = findViewById(R.id.view_pager);
        tabLayout = findViewById(R.id.tab_layout);

        adapter = new MyFragmentAdapter(this);
        viewPager.setAdapter(adapter);

        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            if (position == 0) {
                tab.setText("フレンド");
            } else if (position == 1) {
                tab.setText("保留中");
            }
        }).attach();

        //フレンド画面からメイン画面への遷移
        ImageButton buttonReturn = (ImageButton) findViewById(R.id.buttonReturn);
        buttonReturn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(FriendActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
        ImageButton openDialogButton = findViewById(R.id.buttonAdd);
        openDialogButton.setOnClickListener(v -> showAddFriendDialog(this));
        // onCreate内にViewModelの初期化を追加
        friendSentRequestViewModel = new ViewModelProvider(this).get(FriendSentRequestViewModel.class);


    }
    public void showAddFriendDialog(Context context) {
        Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.dialog_add_friend);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.getWindow().setDimAmount(0.5f);
        EditText editFriendId = dialog.findViewById(R.id.edit_friend_id);
        Button buttonSearch = dialog.findViewById(R.id.button_search);
        ImageView buttonClose = dialog.findViewById(R.id.button_close);

        buttonClose.setOnClickListener(v -> dialog.dismiss());

        buttonSearch.setOnClickListener(v -> {
            String receiverId = editFriendId.getText().toString().trim();
            if (receiverId.isEmpty()) {
                Toast.makeText(context, "IDを入力してください", Toast.LENGTH_SHORT).show();
                return;
            }

            String senderId = tampopo.getUserId(); // TODO: 実際のIDに置き換え
            String token = tampopo.getToken(); // TODO: 実際の認証トークンに置き換え

            friendSentRequestViewModel.sendFriendRequest(senderId, receiverId, token);

            // 成功/失敗メッセージをViewModelから受け取る
            friendSentRequestViewModel.getOperationResult().observe(this, result -> {
                Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
                dialog.dismiss();  // 送信後にダイアログを閉じる
            });
        });

        dialog.show();
    }

}
