package com.fxy.base;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.fxy.baselibrary.base.BaseActivity;
import com.fxy.baselibrary.language.other.LanguageType;
import com.fxy.baselibrary.language.other.MultiLanguageUtil;
import com.fxy.baselibrary.util.StringUtils;
import com.fxy.R;
import com.fxy.constant.SPCache;
import com.fxy.login.LoginActivity;
import com.fxy.main.MainActivity;

/**
 * 设置语言页面
 */
public class SetLanguageActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout rl_followsytem;
    private RelativeLayout rl_simplified_chinese;
    private RelativeLayout rl_traditional_chinese;
    private RelativeLayout rl_english;
    private ImageView iv_english;
    private ImageView iv_followsystem;
    private ImageView iv_simplified_chinese;
    private ImageView iv_traditional_chinese;
    private int savedLanguageType;



    @Override
    public int getContentViewResId() {
        return R.layout.activity_set_language;
    }

    @Override
    public boolean showToolBar() {
        return false;
    }

    @Override
    public boolean openEventBus() {
        return false;
    }

    @Override
    public boolean isPlayMusic() {
        return true;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initView() {
        setTitleName("设置语言");
        rl_followsytem = findViewById(R.id.rl_followsytem);
        rl_simplified_chinese = findViewById(R.id.rl_simplified_chinese);
        rl_traditional_chinese = findViewById(R.id.rl_traditional_chinese);
        rl_english = findViewById(R.id.rl_english);
        iv_followsystem = findViewById(R.id.iv_followsystem);
        iv_english = findViewById(R.id.iv_english);
        iv_simplified_chinese = findViewById(R.id.iv_simplified_chinese);
        iv_traditional_chinese = findViewById(R.id.iv_traditional_chinese);
        rl_followsytem.setOnClickListener(this);
        rl_simplified_chinese.setOnClickListener(this);
        rl_traditional_chinese.setOnClickListener(this);
        rl_english.setOnClickListener(this);
        savedLanguageType = MultiLanguageUtil.getInstance().getLanguageType();
        if (savedLanguageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
            setFollowSytemVisible();
        } else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
            setTraditionalVisible();
        } else if (savedLanguageType == LanguageType.LANGUAGE_EN) {
            setEnglishVisible();
        } else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
            setSimplifiedVisible();
        } else {
            setSimplifiedVisible();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        int selectedLanguage = 0;
        switch (id) {
            case R.id.rl_followsytem:
                setFollowSytemVisible();
                selectedLanguage = LanguageType.LANGUAGE_FOLLOW_SYSTEM;
                break;
            case R.id.rl_simplified_chinese:
                setSimplifiedVisible();
                selectedLanguage = LanguageType.LANGUAGE_CHINESE_SIMPLIFIED;

                break;
            case R.id.rl_traditional_chinese:
                setTraditionalVisible();
                selectedLanguage = LanguageType.LANGUAGE_CHINESE_TRADITIONAL;

                break;
            case R.id.rl_english:
                setEnglishVisible();
                selectedLanguage = LanguageType.LANGUAGE_EN;
                break;
        }
        MultiLanguageUtil.getInstance().updateLanguage(selectedLanguage);
        if (StringUtils.isEmpty(SPCache.getInstance(SetLanguageActivity.this).getToken())) {
            Intent intent = new Intent(SetLanguageActivity.this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }else{
            Intent intent = new Intent(SetLanguageActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }
        if (selectedLanguage == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
            System.exit(0);
        }

    }

    private void setSimplifiedVisible() {
        iv_followsystem.setVisibility(View.GONE);
        iv_english.setVisibility(View.GONE);
        iv_simplified_chinese.setVisibility(View.VISIBLE);
        iv_traditional_chinese.setVisibility(View.GONE);
    }

    private void setEnglishVisible() {
        iv_followsystem.setVisibility(View.GONE);
        iv_english.setVisibility(View.VISIBLE);
        iv_simplified_chinese.setVisibility(View.GONE);
        iv_traditional_chinese.setVisibility(View.GONE);
    }

    private void setTraditionalVisible() {
        iv_followsystem.setVisibility(View.GONE);
        iv_english.setVisibility(View.GONE);
        iv_simplified_chinese.setVisibility(View.GONE);
        iv_traditional_chinese.setVisibility(View.VISIBLE);
    }

    private void setFollowSytemVisible() {
        iv_followsystem.setVisibility(View.VISIBLE);
        iv_english.setVisibility(View.GONE);
        iv_simplified_chinese.setVisibility(View.GONE);
        iv_traditional_chinese.setVisibility(View.GONE);
    }
}
