package com.example.cloudmusic.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.cloudmusic.R;
import com.example.cloudmusic.fragment.MineFragment;
import com.example.cloudmusic.utils.OkHttpUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class FristActivity extends AppCompatActivity {

    private RadioGroup rg_group;
    private FragmentManager manager;
    private MainActivity mainFragment;
    private MineFragment mineFragment;
    private Fragment nowFragemnt;
    private Handler mHandler;
    private FrameLayout fragment2;
    private FrameLayout fragment1;
    private MainActivity mainActivity1;
    private MainActivity mainActivity2;
    private MainActivity mainActivity3;
    private MainActivity mainActivity4;
    private FrameLayout fragment3;
    private FrameLayout fragment4;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frist);
        openShow();
        chooseOne();
        rg_group = findViewById(R.id.rg_group);
        RadioButton rg_one = findViewById(R.id.rg_one);
        rg_group.check(R.id.rg_one);
        rg_group.setOnCheckedChangeListener((radioGroup, i) -> {
            switch (radioGroup.getCheckedRadioButtonId()){
                case R.id.rg_one:
                    findViewById(R.id.first_s).setBackgroundResource(R.color.white);
                    findViewById(R.id.first_b).setVisibility(View.VISIBLE);
                    openShow();
                    chooseOne();
                    break;
                case R.id.rg_two:
                    System.out.println("hello world");
                    closeShow();
                    chooseOne();
                    break;
                case R.id.rg_three:
                    closeShow();
                    startNewPage();
                    startActivity(new Intent(this,PlayerActivity.class));
                    break;
                case R.id.rg_four:
                    closeShow();
                    chooseOne();
                    break;
                case R.id.rg_five:
                    closeShow();
                    findViewById(R.id.first_s).setBackgroundResource(R.color.mineColor);
                    findViewById(R.id.first_b).setVisibility(View.INVISIBLE);
                    chooseFive();
                    break;
            }
        });

        mHandler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        String id = (String) msg.obj;
                        Intent intent = new Intent(FristActivity.this,PlayerActivity.class);
                        Bundle bundle = new Bundle();
                        System.out.println(msg.obj);
                        bundle.putString("id", id);
                        bundle.putString("songListName","漫游");
                        intent.putExtras(bundle);
                        startActivity(intent);
                }
            }
        };
    }

    private void openShow() {
        fragment1 = findViewById(R.id.fragment_1);
        fragment2 = findViewById(R.id.fragment_2);
        fragment3 = findViewById(R.id.fragment_3);
        fragment4 = findViewById(R.id.fragment_4);
        fragment1.setVisibility(View.VISIBLE);
        fragment2.setVisibility(View.VISIBLE);
        fragment3.setVisibility(View.VISIBLE);
        fragment4.setVisibility(View.VISIBLE);
    }
    private void closeShow() {
        fragment1.setVisibility(View.GONE);
        fragment3.setVisibility(View.GONE);
        fragment4.setVisibility(View.GONE);
        fragment2.setVisibility(View.GONE);
    }

    private void startNewPage() {
        OkHttpUtil util = new OkHttpUtil();
        util.sendGetRequest("http://192.168.86.158:3000/personalized/newsong?limit=1", new OkHttpUtil.OkhttpInterface() {
            @Override
            public void success(Call call, Response response) {
                try {
                    String string = response.body().string();
                    JSONObject object = new JSONObject(string);
                    JSONArray array = object.optJSONArray("result");
                    JSONObject json = (JSONObject) array.get(0);
                    String id = json.optString("id");
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = id;
                    mHandler.sendMessage(msg);
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void failure(Call call, IOException e) {

            }
        });
    }
    private void chooseOne() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mainFragment==null){
            mainFragment = new MainActivity();
            mainActivity1 = new MainActivity();
            mainActivity2 = new MainActivity();
            mainActivity3 = new MainActivity();
            mainActivity4 = new MainActivity();
        }
        hideAllFragment(transaction);
        transaction.add(R.id.fragment,mainFragment);
        transaction.add(R.id.fragment_1, mainActivity1);
        transaction.add(R.id.fragment_2, mainActivity2);
        transaction.add(R.id.fragment_3, mainActivity3);
        transaction.add(R.id.fragment_4, mainActivity4);
        //显示Fragment
        transaction.show(mainFragment);
        //记录Fragment
        nowFragemnt = mainFragment;
        //提交事务
        transaction.commit();
    }
    private void chooseFive() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mineFragment==null){
            mineFragment = new MineFragment();
        }
        hideAllFragment(transaction);
        transaction.add(R.id.fragment,mineFragment);
        //显示Fragment
        transaction.show(mineFragment);
        //记录Fragment
        nowFragemnt = mineFragment;
        //提交事务
        transaction.commit();
    }
    private void hideAllFragment(FragmentTransaction transaction){
        if (mainFragment!= null){
            transaction.remove(mainFragment);
            transaction.remove(mainActivity1);
            transaction.remove(mainActivity2);
            transaction.remove(mainActivity3);
            transaction.remove(mainActivity4);
        }
        if (mineFragment!= null){
            transaction.remove(mineFragment);
        }
        if (mainFragment!= null){
            transaction.remove(mainFragment);
        }
        if (mineFragment!= null){
            transaction.remove(mineFragment);
        }
    }
}