package com.example.leudaemialikeme.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.leudaemialikeme.Activity.APNActivity;
import com.example.leudaemialikeme.Activity.AboutActivity;
import com.example.leudaemialikeme.Activity.AttentionActivity;
import com.example.leudaemialikeme.Activity.BaseActivity;
import com.example.leudaemialikeme.Activity.CollectActivity;
import com.example.leudaemialikeme.Activity.ConcernedProblemActivity;
import com.example.leudaemialikeme.Activity.EventActivity;
import com.example.leudaemialikeme.Activity.HistoryActivity;
import com.example.leudaemialikeme.Activity.LoginActivity;
import com.example.leudaemialikeme.Activity.MdctRmdActivity;
import com.example.leudaemialikeme.Activity.MyQuestionActivity;
import com.example.leudaemialikeme.Activity.SafetyActivity;
import com.example.leudaemialikeme.Model.User;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Utils.OkHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class MyFragment extends Fragment {
    private User user;
    private TextView text_nickname;

    public MyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        initClick(view);
        text_nickname = view.findViewById(R.id.nickname);
        getUser();
        return view;
    }

    private void getUser() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    String url = LoginActivity.SERVER_URL+"/my-servlet?action=getUser&username="+ BaseActivity.owner.getUsername();
                    OkHttpUtil.asyGet(url, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }
                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String jsonStr = response.body().string();
                            Gson gson = new Gson();
                            user = gson.fromJson(jsonStr, User.class);
                            getActivity().runOnUiThread(new Runnable(){
                                @Override
                                public void run() {
                                    text_nickname.setText(user.getUsername());
                                }
                            });
                        }
                    });

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initClick(View view) {
        //关注的问题，提醒和大事记的线性布局
        LinearLayout remindLinear = view.findViewById(R.id.my_remind_linear);
        LinearLayout noteLinear = view.findViewById(R.id.my_note_linear);
        LinearLayout questionLinear=view.findViewById(R.id.concern_question_linear);
        LinearLayout myQuestionLinear = view.findViewById(R.id.myQuestion);
        LinearLayout aboutUS = view.findViewById(R.id.aboutUS);
        LinearLayout safety = view.findViewById(R.id.safety);
        LinearLayout collect = view.findViewById(R.id.collect);
        LinearLayout history = view.findViewById(R.id.history);
        //三个TextView 回答，帖子，赞同
        TextView answer = view.findViewById(R.id.my_answer);
        TextView invitation = view.findViewById(R.id.my_invitation);
        TextView consent = view.findViewById(R.id.my_consent);
        TextView attention = view.findViewById(R.id.attention);
        //设置文本点击之后的跳转
        answer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), APNActivity.class);
                startActivity(intent);
            }
        });
        invitation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), APNActivity.class);
                startActivity(intent);
            }
        });
        consent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), APNActivity.class);
                startActivity(intent);
            }
        });
        attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), AttentionActivity.class);
                startActivity(intent);
            }
        });
       //服药提醒的跳转
       remindLinear.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Intent intent = new Intent(getContext(), MdctRmdActivity.class);
               startActivity(intent);
           }
       });
       //大事记的提醒
        noteLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), EventActivity.class);
                startActivity(intent);
            }
        });
        //关注的问题的跳转
        questionLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ConcernedProblemActivity.class);
                startActivity(intent);
            }
        });
        myQuestionLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), MyQuestionActivity.class);
                startActivity(intent);
            }
        });
        aboutUS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), AboutActivity.class);
                startActivity(intent);
            }
        });
        safety.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SafetyActivity.class);
                startActivity(intent);
            }
        });
        collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), CollectActivity.class);
                startActivity(intent);
            }
        });
        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), HistoryActivity.class);
                startActivity(intent);
            }
        });

    }
}