package org.ntlab.acanthus_client.views.userpage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.jetbrains.annotations.Nullable;
import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.entities.FollowUidJson;
import org.ntlab.acanthus_client.views.userpage.followList.FollowListActivity;
import org.ntlab.acanthus_client.views.userpage.followerList.FollowerListActivity;
import org.ntlab.acanthus_client.views.userpage.workList.WorkListActivity;


import java.util.ArrayList;
import java.util.Collection;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

public class UserPageActivity extends AppCompatActivity {

    private UserPageViewModel userPageViewModel;
    private Acanthus acanthus;
    private Collection<Integer> followsList = new ArrayList<>();

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

        userPageViewModel = new ViewModelProvider(this).get(UserPageViewModel.class);
        acanthus = (Acanthus) getApplication();
        Integer uid = 1;
        String token = "abc0";
        Integer uid2 = 2;

        TextView userNameText = findViewById(R.id.userNameText);
//        userNameText.setText();

        TextView profileText = findViewById(R.id.profileText);

        Button returnButton = findViewById(R.id.returnButton);

        //前の画面に戻る処理
        returnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Button followUserButton = findViewById(R.id.followUserButton);
        //ログインしているユーザーがフォローしているかの有無で画面表示時のボタンの文字を変更する
        userPageViewModel.getFollows(uid);
        userPageViewModel.getFollowUidJson().observe(this, new Observer<FollowUidJson>() {
            @Override
            public void onChanged(@Nullable FollowUidJson followUidJson) {
                followsList = followUidJson.getFollowsUid();

                if (followsList.contains(uid2)) {
                    followUserButton.setText("フォロー解除");
                }else{
                    followUserButton.setText("フォローする");
                }

            }
        });

        if (followsList.contains(uid2)) {
            followUserButton.setText("フォロー解除");
        }else{
            followUserButton.setText("フォローする");
        }

        //ユーザーのフォロー及びフォロー解除の処理
        followUserButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!followsList.contains(uid2)) {
                    userPageViewModel.addFollow(uid, token, uid2);
                    followUserButton.setText("フォロー解除");
                }else{
                    userPageViewModel.releaseFollow(uid, token, uid2);
                    followUserButton.setText("フォローする");
                }
            }
        });

        //ユーザーのフォロー一覧への画面遷移の処理
        Button followListButton = findViewById(R.id.followListButton);
        followListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplication(), FollowListActivity.class);
                startActivity(intent);
            }
        });

        //ユーザーのフォロワー一覧への画面遷移の処理
        Button followerListButton = findViewById(R.id.followerListButton);
        followerListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplication(), FollowerListActivity.class);
                startActivity(intent);
            }
        });

        //ユーザーの作品一覧への画面遷移の処理
        Button workListButton = findViewById(R.id.workListButton);
        workListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplication(), WorkListActivity.class);
                startActivity(intent);
            }
        });

    }

}
