package com.example.sleep;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;

import java.util.List;

public class DreamAnalyszeScreen extends Fragment {
    private Button startSleep;//开始按钮
    private FragmentActivity activity;
    private TextView dreamDes;//用于显示梦境解答
    private EditText dreamtTitle;//用于输入梦境关键词
    private ProgressBar progress;
    private Gson gson = new Gson();// 用于处理JSON数据

    private Handler handler = new Handler(Looper.getMainLooper())//// 创建Handler对象，用于处理从网络请求返回的消息
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            DreamBean dreamResponse = gson.fromJson(data, DreamBean.class);//将JSON数据转换为DreamBean对象
            DreamBean.Result result = dreamResponse.getResult();
            if (result == null) //如果没有结果，提示“对不起没有收录此梦境”
            {
                progress.setVisibility(View.GONE);
                startSleep.setVisibility(View.VISIBLE);
                Toast.makeText(activity,"对不起没有收录此梦境",Toast.LENGTH_SHORT).show();
                return;
            }
            List<DreamBean.Dream> dreams = result.getList();//获取梦境列表
            for (DreamBean.Dream dream : dreams)//进行梦境解答
            {
                String result1 = dream.getResult();
                // dreamDes.setText(result1);
                //显示梦境解答结果
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                {
                    dreamDes.setText(Html.fromHtml(result1, Html.FROM_HTML_MODE_LEGACY));
                } else {
                    dreamDes.setText(result1);
                }
                progress.setVisibility(View.GONE);
                startSleep.setVisibility(View.VISIBLE);
                return;
            }
            //若没有结果，提示“对不起没有收录此梦境”
            progress.setVisibility(View.GONE);
            startSleep.setVisibility(View.VISIBLE);
            Toast.makeText(activity,"对不起没有收录此梦境",Toast.LENGTH_SHORT).show();
        }
    };

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 在这里设置 SleepFragment 的布局，例如从 XML 文件中加载
        View view = inflater.inflate(R.layout.fragment_dream, container, false);
        startSleep = view.findViewById(R.id.btn_start);
        dreamDes = view.findViewById(R.id.dream_des);
        dreamtTitle = view.findViewById(R.id.dream_title);
        progress = view.findViewById(R.id.progress_bar);
        initListener();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // 在这里获取关联的Activity上下文
        activity = getActivity();
    }

    private void initListener() {
        startSleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keywords = dreamtTitle.getText().toString();//获取输入的梦境关键词
                if (TextUtils.isEmpty(keywords))//若关键词为空，显示提示信息
                {
                    Toast.makeText(activity,"请输入梦境",Toast.LENGTH_SHORT).show();
                    return;
                }
                startSleep.setVisibility(View.GONE);
                progress.setVisibility(View.VISIBLE);
                new NetWork().getDream(keywords,handler);// 通过NetWork类的getDream方法获取梦境解答，并通过Handler处理返回的消息
            }
        });
    }

}