package com.sunzee.ui.fragment;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.sunzee.R;
import com.sunzee.base.MvpFragment;
import com.sunzee.model.domain.Name;
import com.sunzee.mvp.languageset.LanguageSetPresenter;
import com.sunzee.mvp.languageset.LanguageSetView;
import com.sunzee.utils.LanguageUtils;
import com.sunzee.utils.SharedPreferencesUtils;
import com.sunzee.utils.ToastUtil;
import com.sunzee.utils.UiUtil;

import java.util.Locale;

/**
 * 语言设置界面 fragment
 */
public class LanguageSetFragment extends MvpFragment<LanguageSetPresenter> implements LanguageSetView, RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private RadioGroup rgLanguage1;
    private static final String TAG = "LanguageSetFragment";
    private Button btnReset;
    private volatile String languageValue = "";
    private RadioButton rbChinese;
    private RadioButton rbEnglish;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_language, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        rgLanguage1 = inflate.findViewById(R.id.rg_language1);
        btnReset = inflate.findViewById(R.id.btn_reset);
        rbChinese = inflate.findViewById(R.id.rb_chinese);
        rbEnglish = inflate.findViewById(R.id.rb_english);
        rgLanguage1.setOnCheckedChangeListener(this);
        btnReset.setOnClickListener(this);
        //之前设置好的语言是哪个吗？
        selectLanguage();

    }

    private void selectLanguage() {
        String languageType = (String)SharedPreferencesUtils.getParam(Name.LANGUAGE_TYPE,"ch");
        switch (languageType) {
            case "ch":
                rbChinese.setChecked(true);
                break;
            case "en":
                rbEnglish.setChecked(true);
                break;
        }
    }

    @Override
    protected LanguageSetPresenter createPresenter() {
        return new LanguageSetPresenter(this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_chinese:
                Log.d(TAG, "onCheckedChanged: 中文");
                languageValue = "ch";
                break;
            case R.id.rb_english:
                Log.d(TAG, "onCheckedChanged: 英文");
                languageValue = "en";
                break;
            default:
                languageValue = "ch";
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reset:
                SharedPreferencesUtils.setParam(Name.LANGUAGE_TYPE,languageValue);
                LanguageUtils.set(languageValue);
                ToastUtil.showToast(UiUtil.getStringRes(R.string.modified_success));
                break;
        }
    }
}
