package org.ntlab.acanthus_client.views.title;

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

import com.bumptech.glide.Glide;

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

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;

import org.ntlab.acanthus_client.databinding.ActivityTitleBinding;
import org.ntlab.acanthus_client.views.MainActivity;
import org.ntlab.acanthus_client.views.sign_up.SignUpActivity;
import org.w3c.dom.Text;

public class TitleActivity extends AppCompatActivity {
    private TitleViewModel titleViewModel;
    private ActivityTitleBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme_NoTitleBar);//タイトルバーを消す
        setContentView(R.layout.activity_title);
        //ここに画像処理を記載する
        ImageView imageView = (ImageView) findViewById(R.id.gifImage);
        Glide.with(this).load(R.drawable.load).into(imageView);
        loadimg(imageView);



        init();
        HandlerTest();
    }

    ////////////////////////////////////////////////////////////////////
    public void loadimg(ImageView imageView) {
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                imageView.setVisibility(View.VISIBLE);
            }
        };
        handler.postDelayed(runnable, 2000);
    }



    //////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////
    public void HandlerTest() {
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Acanthus acanthus = (Acanthus) getApplication();
                titleViewModel.LoginToken(acanthus, acanthus.getPreferenceEmail(), acanthus.getPreferencePassword());
            }
        };
        handler.postDelayed(runnable, 10000);
    }

    /////////////////////////////////////////////////////////////
    private void init() {
        binding = ActivityTitleBinding.inflate(getLayoutInflater());
        startObserve();
        falseLoginObserve();
    }

    // VMの監視開始
    //3秒間の画面表示
    private void startObserve() {
        titleViewModel = new ViewModelProvider(this).get(TitleViewModel.class);
        titleViewModel.getAcanthus().observe(this, new Observer<Acanthus>() {
            @Override
            public void onChanged(Acanthus acanthus) {
                succeedLogin();
            }
        });
    }

    private void falseLoginObserve() {
        titleViewModel = new ViewModelProvider(this).get(TitleViewModel.class);
        titleViewModel.getLoginStatus().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean loginStatus) {
                falseLogin();
            }
        });
    }

    private void succeedLogin() {
        transitionActivity(new MainActivity());
    }

    private void falseLogin() {
        transitionActivity(new SignUpActivity());
    }

    private void transitionActivity(AppCompatActivity appCompatActivity) {
        Intent intent = new Intent(getApplication(), appCompatActivity.getClass());
        startActivity(intent);
        finish();
    }


}