package epson.support;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import androidx.annotation.NonNull;

import com.epson.mobilephone.common.guide.NoticeGuideActivity;
import com.epson.mobilephone.common.license.InfoDisplayActivity;
import com.epson.mobilephone.common.license.UserSurveyInvitationActivity;

import epson.common.InformationGuide;
import epson.common.IprintLicenseInfo;
import epson.common.IprintUserSurveyInfo;
import epson.print.ActivityIACommon;
import epson.print.R;
import epson.print.CommonDefine;
import epson.print.ThreeButtonDialog;
import epson.print.Util.BuyInkUrl;

public class SupportActivity extends ActivityIACommon implements CommonDefine, View.OnClickListener, ThreeButtonDialog.DialogCallback {
    private static final int REQUEST_CODE_USER_SURVEY = 1;
    private static final String TUTORIAL_URL = "https://www.epsonconnect.com/iguide/";
    private SwitchAdapter mUserSurveySwitch;

    public void callback(int i) {
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main_support);

        setActionBar(R.string.title_support, true);
        findViewById(R.id.linearLayoutIprintNews).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                InformationGuide informationGuide = new InformationGuide(getApplicationContext());
                Intent intent = new Intent(SupportActivity.this, NoticeGuideActivity.class);
                intent.putExtra(NoticeGuideActivity.KEY_HTML_PATH, informationGuide.getHtmlPath());
                intent.putExtra(NoticeGuideActivity.KEY_GUIDE_VER, InformationGuide.GUIDE_VER);
                intent.putExtra(NoticeGuideActivity.KEY_BOOT_MODE, NoticeGuideActivity.MODE_MANUAL);
                startActivity(intent);
            }
        });
        final String faqUrlString = BuyInkUrl.getFaqUrlString(this);
        findViewById(R.id.linearLayoutFAQ).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent("android.intent.action.VIEW", Uri.parse(faqUrlString)));
            }
        });
        findViewById(R.id.linearLayoutTutorial).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent("android.intent.action.VIEW", Uri.parse(SupportActivity.TUTORIAL_URL)));
            }
        });
        mUserSurveySwitch = new SwitchAdapter((Switch) findViewById(R.id.tb_send_usageinformation), new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
                onCheckChanged(z);
            }
        });
        updateUserSurveySwitch();
        findViewById(R.id.linearLayoutPrivacy).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startDocumentDisplayActivity(2);
            }
        });
        findViewById(R.id.linearLayoutAbout).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(SupportActivity.this, SupportAboutActivity.class));
            }
        });
        findViewById(R.id.linearLayoutLicense).setOnClickListener(this);
        findViewById(R.id.ossLicenseLayout).setOnClickListener(this);
        findViewById(R.id.linearLayoutDeviceInformation).setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                startActivity(new Intent(SupportActivity.this, DeviceInformationActivity.class));
            }
        });
    }

    private void onCheckChanged(boolean z) {
        IprintUserSurveyInfo iprintUserSurveyInfo = new IprintUserSurveyInfo();
        if (!z) {
            iprintUserSurveyInfo.setUserSurveyAgreement(this, false);
        } else {
            startUserSurveyInvitationActivity();
        }
    }

    private void startUserSurveyInvitationActivity() {
        startActivityForResult(UserSurveyInvitationActivity.getStartIntent(this, new IprintUserSurveyInfo(), new IprintLicenseInfo()), 1);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.linearLayoutLicense) {
            startDocumentDisplayActivity(1);
        } else if (view.getId() == R.id.ossLicenseLayout) {
            startDocumentDisplayActivity(3);
        }
    }

    private void startDocumentDisplayActivity(int i) {
        startActivity(InfoDisplayActivity.getStartIntent(this, new IprintLicenseInfo(), i));
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i == 1) {
            updateUserSurveySwitch();
        }
    }

    private void updateUserSurveySwitch() {
        mUserSurveySwitch.setCheckWithoutCallback(new IprintUserSurveyInfo().isUserSurveyEnabled(this));
    }

    protected void onPause() {
        super.onPause();
        mUserSurveySwitch.setListenerEnabled(false);
    }

    protected void onResume() {
        super.onResume();
        mUserSurveySwitch.setListenerEnabled(true);
    }

    private static class SwitchAdapter implements CompoundButton.OnCheckedChangeListener {
        private boolean mOriginalChangeListenerEnabled = false;
        private CompoundButton.OnCheckedChangeListener mOriginalListener;
        private Switch mSwitch;

        public SwitchAdapter(@NonNull Switch switchR, CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
            mSwitch = switchR;
            mOriginalListener = onCheckedChangeListener;
            switchR.setOnCheckedChangeListener(this);
        }

        public void setListenerEnabled(boolean z) {
            mOriginalChangeListenerEnabled = z;
        }

        public void setCheckWithoutCallback(boolean z) {
            boolean z2 = mOriginalChangeListenerEnabled;
            mOriginalChangeListenerEnabled = false;
            mSwitch.setChecked(z);
            mOriginalChangeListenerEnabled = z2;
        }

        public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
            CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
            if (mOriginalChangeListenerEnabled && (onCheckedChangeListener = mOriginalListener) != null) {
                onCheckedChangeListener.onCheckedChanged(compoundButton, z);
            }
        }
    }
}
