package jp.gihyo.mitoma.android.s0202001;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private int firstSize;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.textView);
        /* ******************************************
        テストによって変数　testの値を変えて再実行
        ******************************************* */
        int test = 3;
        if(test == 1){
            // Step1 テキストの表示文字変換
            textView.setOnClickListener(new ChangeTextListener());
        }else if(test == 2){
            // Step2 テキストの表示文字と背景色の変更
            textView.setOnClickListener(new ChangeBackColorListener());
        }else if(test ==3){
            // Step3 テキストの表示非表示を変える
            textView.setOnClickListener(new ChangeVisibilityListener());
            ImageView imageView = findViewById(R.id.imageView);
            imageView.setOnClickListener(new ChangeVisibilityListener());
        }else{
            // NOP
        }
    }
    // Step1 テキストの表示文字変換
    private class ChangeTextListener implements View.OnClickListener{
        public void onClick(View view){
            int no=view.getId();
            TextView textView = findViewById(no);
            String nowText = (String)textView.getText();
            if(nowText.equals(getResources().getString(R.string.text_ask))){
                textView.setText(getResources().getString(R.string.text_resp));
            }else{
                textView.setText(getResources().getString(R.string.text_ask));
            }
        }
    }
    // Step2 テキストの表示文字と背景色の変更
    private class ChangeBackColorListener implements View.OnClickListener{
        public void onClick(View view){
            int no=view.getId();
            TextView textView = findViewById(no);
            int nowColor= textView.getTextColors().getDefaultColor();
            if(nowColor == Color.BLACK){
                textView.setBackgroundColor(Color.BLUE);
                textView.setTextColor(Color.WHITE);
            }else{
                textView.setBackgroundColor(Color.WHITE);
                textView.setTextColor(Color.BLACK);
            }
        }
    }
    // Step3 テキストの表示非表示を変える
    private class ChangeVisibilityListener implements View.OnClickListener{
        public void onClick(View view){
            int no=view.getId();
            TextView textView1 = findViewById(R.id.textView);
            ImageView imageView1 = findViewById(R.id.imageView);
            if(no == getResources().getIdentifier
                    ("textView", "id", getPackageName())){
                textView1.setVisibility(View.INVISIBLE);
                imageView1.setVisibility(View.VISIBLE);
            }else{
                textView1.setVisibility(View.VISIBLE);
                imageView1.setVisibility(View.INVISIBLE);
            }
        }
    }
}