package com.tofly.ghrq_route.common.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.DialogTitle;
import androidx.appcompat.widget.SwitchCompat;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.common.util.sharedPreference.SharePreUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * Created by Amu on 2017/2/13.
 */

public class UiMainLayerManagerDailog extends AlertDialog {
    @BindView(android.R.id.icon)
    ImageView icon;
    @BindView(R.id.alertTitle)
    DialogTitle alertTitle;
    @BindView(R.id.radio_group_map)
    RadioGroup radioGroupMap;
    @BindView(R.id.radio_btn_online)
    RadioButton radioButtonOnline;
    @BindView(R.id.radio_btn_offline)
    RadioButton radioButtonOffline;
    @BindView(R.id.toggle_basemap)
    SwitchCompat switchCompatBaseMap;
    @BindView(R.id.toggle_satellite)
    SwitchCompat switchCompatSatellite;
    @BindView(R.id.toggle_pipe)
    SwitchCompat switchCompatPipe;
    @BindView(R.id.layout_poi)
    RelativeLayout layoutPoi;
    @BindView(R.id.toggle_poi)
    SwitchCompat switchCompatPoi;
    @BindView(R.id.toggle_sign)
    SwitchCompat switchCompatSignLayer;
    @BindView(android.R.id.button2)
    Button button2;
    @BindView(android.R.id.button1)
    Button button1;

    private Context mContext;

    private ClickListenerInterface mClickListenerInterface;

    public UiMainLayerManagerDailog(Context context) {
        super(context, R.style.AlertDialog);
        mContext = context;
    }

    public interface ClickListenerInterface {
        public void doOk(boolean isOnline, boolean bBaseMap, boolean bPipe, boolean satellite,boolean sign);

        public void doCancel();
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.mClickListenerInterface = clickListenerInterface;
    }


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

        setContentView(R.layout.dialog_main_layer_manager);
        ButterKnife.bind(this);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);

        initView();
    }

    private void initView() {
        alertTitle.setText(R.string.pop_layoutmanager);
        boolean onlineMap = isOnlineMap();
        radioGroupMap.check(onlineMap ? R.id.radio_btn_online : R.id.radio_btn_offline);

        layoutPoi.setVisibility(onlineMap ? View.VISIBLE : View.GONE);

        boolean isBaseMapOn = isBaseMapLayerOn();
        boolean isPipeOn = isPipeLayerOn();
        boolean satelliteLayerOn = isSatelliteLayerOn();
        boolean isPoiOn = isPoiLayerOn();
        boolean isSignOn = isSignLayerOn();
        switchCompatBaseMap.setChecked(isBaseMapOn);
        switchCompatPipe.setChecked(isPipeOn);
        switchCompatSatellite.setChecked(satelliteLayerOn);
        switchCompatPoi.setChecked(isPoiOn);
        switchCompatSignLayer.setChecked(isSignOn);
        button1.setText(android.R.string.ok);
        button2.setText(android.R.string.cancel);
    }

    public void setSignLayerState(boolean state) {
        switchCompatSignLayer.setChecked(state);
    }

    @OnClick({android.R.id.button1, android.R.id.button2})
    public void onClick(View view) {
        switch (view.getId()) {
            /*Button OK*/
            case android.R.id.button1:
                if (mClickListenerInterface != null) {
                    boolean onlineMap = radioButtonOnline.isChecked();
                    boolean isBaseMapOn = switchCompatBaseMap.isChecked();
                    boolean isPipeOn = switchCompatPipe.isChecked();
                    boolean isPoiOn = switchCompatPoi.isChecked();
                    boolean isImageOn = switchCompatSatellite.isChecked();
                    boolean isSignOn = switchCompatSignLayer.isChecked();
                    setOnlineMap(onlineMap);
                    setIsBaseMapLayerOn(isBaseMapOn);//保存底图开关的状态
                    setIsSatelliteLayerOn(isImageOn);  //保存影像开关状态
                    setIsPipeLayerOn(isPipeOn);//保存管线开关的状态
                    setIsPoiLayerOn(isPoiOn);  //保存地名开关状态
                    setIsSignLayerOn(isSignOn);  //保存轨迹图层开关状态
                    mClickListenerInterface.doOk(onlineMap, isBaseMapOn, isPipeOn
                            , isImageOn
                            , switchCompatSignLayer.isChecked());
                }
                this.dismiss();
                break;
            case android.R.id.button2:
                this.dismiss();
                break;
        }
    }

    @OnCheckedChanged({R.id.toggle_basemap, R.id.toggle_satellite, R.id.radio_btn_online, R.id.radio_btn_offline})
    public void onRadioCheck(CompoundButton view, boolean ischanged){
        switch (view.getId()) {
            case R.id.toggle_basemap:
                onBasemapToggled(true, ischanged);
                break;
            case R.id.toggle_satellite:
                onBasemapToggled(false, ischanged);
                break;
            case R.id.radio_btn_online:
                if (ischanged)
                    layoutPoi.setVisibility(View.VISIBLE);
                break;
            case R.id.radio_btn_offline:
                if (ischanged)
                    layoutPoi.setVisibility(View.GONE);
                break;
        }
    }

    private void onBasemapToggled(boolean isTpk, boolean isOn) {
        if (isOn) {
            if (isTpk) {
                switchCompatSatellite.setChecked(false);
            } else {
                switchCompatBaseMap.setChecked(false);
            }
        }
    }

    public static boolean isOnlineMap() {
        return SharePreUtil.getIsOnlineMap(BaseApp.getInstance());
    }

    public static void setOnlineMap(boolean set) {
        SharePreUtil.setOnlineMap(BaseApp.getInstance(), set);
    }

    public static boolean isBaseMapLayerOn() {
        return SharePreUtil.getIsLayoutOn(BaseApp.getInstance(), SharePreUtil.LOCALTILEDLAYER);
    }

    public static boolean isPipeLayerOn() {
        return SharePreUtil.getIsLayoutOn(BaseApp.getInstance(), SharePreUtil.GEODATABASE);
    }

    public static boolean isSatelliteLayerOn() {
        return SharePreUtil.getIsLayoutOn(BaseApp.getInstance(), SharePreUtil.SATELLITE);
    }

    public static boolean isPoiLayerOn() {
        return SharePreUtil.getIsLayoutOn(BaseApp.getInstance(), SharePreUtil.POILAYER);
    }

    public static boolean isSignLayerOn() {
        return SharePreUtil.getIsLayoutOn(BaseApp.getInstance(), SharePreUtil.SIGN_LAYER);
    }

    public static void setIsBaseMapLayerOn(boolean set) {
        SharePreUtil.setIsLayoutOn(BaseApp.getInstance(), SharePreUtil.LOCALTILEDLAYER, set);
    }

    public static void setIsPipeLayerOn(boolean set) {
        SharePreUtil.setIsLayoutOn(BaseApp.getInstance(), SharePreUtil.GEODATABASE, set);
    }

    public static void setIsSatelliteLayerOn(boolean set) {
        SharePreUtil.setIsLayoutOn(BaseApp.getInstance(), SharePreUtil.SATELLITE, set);
    }

    public static void setIsPoiLayerOn(boolean set) {
        SharePreUtil.setIsLayoutOn(BaseApp.getInstance(), SharePreUtil.POILAYER, set);
    }

    public static void setIsSignLayerOn(boolean set) {
        SharePreUtil.setIsLayoutOn(BaseApp.getInstance(), SharePreUtil.SIGN_LAYER, set);
    }
}
